Runtime inspection utilities for Python typing module

Overview

Typing Inspect

Build Status

The typing_inspect module defines experimental API for runtime inspection of types defined in the Python standard typing module. Works with typing version 3.7.4 and later. Example usage:

from typing import Generic, TypeVar, Iterable, Mapping, Union
from typing_inspect import is_generic_type

T = TypeVar('T')

class MyCollection(Generic[T]):
    content: T

assert is_generic_type(Mapping)
assert is_generic_type(Iterable[int])
assert is_generic_type(MyCollection[T])

assert not is_generic_type(int)
assert not is_generic_type(Union[int, T])

Note: The API is still experimental, if you have ideas/suggestions please open an issue on tracker. Currently typing_inspect only supports latest version of typing. This limitation may be lifted if such requests will appear frequently.

Currently provided functions (see functions docstrings for examples of usage):

  • is_generic_type(tp): Test if tp is a generic type. This includes Generic itself, but excludes special typing constructs such as Union, Tuple, Callable, ClassVar.
  • is_callable_type(tp): Test tp is a generic callable type, including subclasses excluding non-generic types and callables.
  • is_tuple_type(tp): Test if tp is a generic tuple type, including subclasses excluding non-generic classes.
  • is_union_type(tp): Test if tp is a union type.
  • is_optional_type(tp): Test if tp is an optional type (either type(None) or a direct union to it such as in Optional[int]). Nesting and TypeVars are not unfolded/inspected in this process.
  • is_literal_type(tp): Test if tp is a literal type.
  • is_final_type(tp): Test if tp is a final type.
  • is_typevar(tp): Test if tp represents a type variable.
  • is_new_type(tp): Test if tp represents a distinct type.
  • is_classvar(tp): Test if tp represents a class variable.
  • get_origin(tp): Get the unsubscripted version of tp. Supports generic types, Union, Callable, and Tuple. Returns None for unsupported types.
  • get_last_origin(tp): Get the last base of (multiply) subscripted type tp. Supports generic types, Union, Callable, and Tuple. Returns None for unsupported types.
  • get_parameters(tp): Return type parameters of a parameterizable type tp as a tuple in lexicographic order. Parameterizable types are generic types, unions, tuple types and callable types.
  • get_args(tp, evaluate=False): Get type arguments of tp with all substitutions performed. For unions, basic simplifications used by Union constructor are performed. If evaluate is False (default), report result as nested tuple, this matches the internal representation of types. If evaluate is True, then all type parameters are applied (this could be time and memory expensive).
  • get_last_args(tp): Get last arguments of (multiply) subscripted type tp. Parameters for Callable are flattened.
  • get_generic_type(obj): Get the generic type of obj if possible, or its runtime class otherwise.
  • get_generic_bases(tp): Get generic base types of tp or empty tuple if not possible.
  • typed_dict_keys(td): Get TypedDict keys and their types, or None if td is not a typed dict.
Owner
Ivan Levkivskyi
Ivan Levkivskyi
A Notifier Program that Notifies you to relax your eyes Every 15 Minutes👀

Every 15 Minutes is an application that is used to Notify you to Relax your eyes Every 15 Minutes, This is fully made with Python and also with the us

Ashely Sato 1 Nov 02, 2021
Android Blobs Organizer

Android Blobs Organizer

Sebastiano Barezzi 96 Jan 02, 2023
ChainJacking is a tool to find which of your Go lang direct GitHub dependencies is susceptible to ChainJacking attack.

ChainJacking is a tool to find which of your Go lang direct GitHub dependencies is susceptible to ChainJacking attack.

Checkmarx 36 Nov 02, 2022
FCurve-Cleaner: Tries to clean your dense mocap graphs like an animator would

Tries to clean your dense mocap graphs like an animator would! So it will produce a usable artist friendly result while maintaining the original graph.

wiSHFul97 5 Aug 17, 2022
A Notifier Program that Notifies you to relax your eyes Every 15 Minutes👀

Every 15 Minutes is an application that is used to Notify you to Relax your eyes Every 15 Minutes, This is fully made with Python and also with the us

FSP Gang s' YT 2 Nov 11, 2021
The ROS publisher/subscriber example packaged as a snap

publisher-subscriber The ROS publisher/subscriber example packaged as a snap, based on ROS Noetic and Ubuntu Core 20. Strictly confined. This example

3 Dec 03, 2021
Controller state monitor plugin for EVA ICS

eva-plugin-cmon Controller status monitor plugin for EVA ICS Monitors connected controllers status in SFA and pushes measurements into an external Inf

Altertech 1 Nov 06, 2021
A set of decks and notebooks with exercises for use in a hands-on causal inference tutorial session

intro-to-causal-inference A introduction to causal inference using common tools from the python data stack Table of Contents Getting Started Install g

Roni Kobrosly 15 Dec 07, 2022
API Rate Limit Decorator

ratelimit APIs are a very common way to interact with web services. As the need to consume data grows, so does the number of API calls necessary to re

Tomas Basham 574 Dec 26, 2022
create cohort visualizations for a subscription business

pycohort The main revenue generator for subscription businesses is recurring payments. There might be additional one-time offerings but the number of

Yalim Demirkesen 4 Sep 09, 2022
A basic python project which replicates the functionalities on an 8 Ball.

Magic-8-Ball To the people who wish to make decisions using a Magic 8 Ball but can't get one? I gotchu. This is a basic python project which replicate

3 Jun 24, 2021
Badge-Link-Creater 'For more beautiful profiles.'

Badge-Link-Creater 'For more beautiful profiles.' Ready Badges Prepares the codes of the previously prepared badges for you. Note Click here for more

Mücahit Gündüz 9 Oct 19, 2022
A simple, light-weight and highly maintainable online judge system for secondary education

y³OJ a simple, light-weight and highly maintainable online judge system for secondary education 一个简单、轻量化、易于维护的、为中学信息技术学科课业教学设计的 Online Judge 系统。 Onlin

20 Oct 04, 2022
Make creating Excel XLSX files fun again

Poi: Make creating Excel XLSX files fun again. Poi helps you write Excel sheet in a declarative way, ensuring you have a better Excel writing experien

Ryan Wang 11 Apr 01, 2022
The LiberaPay archive module for the SeanPM life archive project.

By: Top README.md Read this article in a different language Sorted by: A-Z Sorting options unavailable ( af Afrikaans Afrikaans | sq Shqiptare Albania

Sean P. Myrick V19.1.7.2 1 Aug 26, 2022
Frappe app for authentication, can be used with FrappeVue-AdminLTE

Frappeauth App Frappe app for authentication, can be used with FrappeVue-AdminLTE

Anthony C. Emmanuel 9 Apr 13, 2022
Hello, Welcome to this repo. don't forget to read guidelines in readme.md

Hacktoberfest_2021 If you looking for your first contribution, we are here to help. Just create a simple program using any language you like in our fo

Wafa Rifqi Anafin 117 Dec 14, 2022
Using Python to parse through email logs received through several backup systems.

outlook-automated-backup-control Backup monitoring on a mailbox: In this mailbox there will be backup logs. The identification will based on the follo

Connor 2 Sep 28, 2022
Reference management solution using Python and Notion.

notion-scholar Reference management solution using Python and Notion. The main idea of this app is to allow to furnish a Notion database using a BibTe

Thomas Hirtz 69 Dec 21, 2022
🤖🧭Creates google-like navigation menu using python-telegram-bot wrapper

python telegram bot menu pagination Makes a google style pagination line for a list of items. In other words it builds a menu for navigation if you ha

Sergey Smirnov 9 Nov 27, 2022