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
This is a working model for which I have used python.

Jarvis_voiceAssistance This is a working model for which I have used python. This model can: 1)Play a video or song on youtube. 2)Tell us time. 3)Tell

Hardik Jain 1 Jan 30, 2022
Keyboard Layout Change - Extension for Ulauncher

Keyboard Layout Change - Extension for Ulauncher

Marco Borchi 4 Aug 26, 2022
Covid-ml-predictors - COVID predictions using AI.

COVID Predictions This repo contains ML models to be trained on COVID-19 data from the UK, sourced off of Kaggle here. This uses many different ML mod

1 Jan 09, 2022
Multi View Stereo on Internet Images

Evaluating MVS in a CPC Scenario This repository contains the set of artficats used for the ENGN8601/8602 research project. The thesis emphasizes on t

Namas Bhandari 1 Nov 10, 2021
Python-Kite: Simple python code to make kite pattern

Python-Kite Simple python code to make kite pattern. Getting Started These instr

Anoint 0 Mar 22, 2022
Change ACLs for QNAP LXD unprivileged container.

qnaplxdunpriv If Advanced Folder Permissions is enabled in QNAP NAS, unprivileged LXD containers won't start. qnaplxdunpriv changes ACLs of some Conta

1 Jan 10, 2022
Traditionally, there is considerable friction for developers when setting up development environments

This self-led, half-day training will teach participants the patterns and best practices for working with GitHub Codespaces

CSE Labs at Spark 12 Dec 02, 2022
Developing a python based app prototype with KivyMD framework for a competition :))

Developing a python based app prototype with KivyMD framework for a competition :))

Jay Desale 1 Jan 10, 2022
The third home of the bare Programming Language (1st there's my heart, the forest came second and then there's Github :)

The third home of the bare Programming Language (1st there's my heart, the forest came second and then there's Github :)

Garren Souza 7 Dec 24, 2022
Multtable is a collection of multiplication table generators in various languages.

Multtable Multtable is a collection of multiplication table generators in various languages. This project was created as a joke based on one of my bro

pollen__ 7 Mar 05, 2022
SQL centered, docker process running game

REQUIREMENTS Linux Docker Python/bash set up image "docker build -t game ." create db container "run my_whatever/game_docker/pdb create" # creating po

1 Jan 11, 2022
A simple solution for water overflow problem in Python

Water Overflow problem There is a stack of water glasses in a form of triangle as illustrated. Each glass has a 250ml capacity. When a liquid is poure

Kris 2 Oct 22, 2021
An animal facts python module

An animal facts python module

Fayas Noushad 3 Dec 19, 2021
Q-Tracker is originally a High School Project created by Admins of Cirus Lab.

Q-Tracker is originally a High School Project created by Admins of Cirus Lab. It's completly coded in python along with mysql.(Tkinter For GUI)

Adithya Krishnan 2 Nov 14, 2022
Stack-overflow-import - Import arbitrary code from Stack Overflow as Python modules.

StackOverflow Importer Do you ever feel like all you’re doing is copy/pasting from Stack Overflow? Let’s take it one step further. from stackoverflow

Filip Haglund 3.7k Jan 08, 2023
PORTSCANNING-IN-PYTHON - A python threaded portscanner to scan websites and ipaddresses

PORTSCANNING-IN-PYTHON This is a python threaded portscanner to scan websites an

1 Feb 16, 2022
Assignment for python course, BUPT 2021.

pyFuujinrokuDestiny Assignment for python course, BUPT 2021. Notice username and password must be ASCII encoding. If username exists in database, syst

Ellias Kiri Stuart 3 Jun 18, 2021
Master Duel Card Translator Project

Master Duel Card Translator Project A tool for translating card effects in Yu-Gi-Oh! Master Duel. Quick Start (for Chinese version only) Download the

67 Dec 23, 2022
An easy way to access the Scratch API!

The majority of people are likely here because they want to easily access the Scratch API!

rgantzos 0 May 04, 2022
Python Service for MISP Feed Management

Python Service for MISP Feed Management This set of scripts is designed to offer better reliability and more control over the fetching of feeds into M

Chris 7 Aug 24, 2022