Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Functionality for types

Project description

image Build Status Pypi version codecov

Typish

  • Functions for thorough checks on types
  • Instance checks considering generics
  • Typesafe Duck-typing

Installation

pip install typish

Content

Functions

Function Description
subclass_of(cls: type, *args: type) -> bool Returns whether cls is a sub type of all types in args
instance_of(obj: object, *args: type) -> bool Returns whether cls is an instance of all types in args
get_origin Return the "origin" of a generic type. E.g. get_origin(List[str]) gives list.
get_args Return the arguments of a generic type. E.g. get_args(List[str]) gives (str, ).
get_alias Return the typing alias for a type. E.g get_alias(list) gives List.
get_type Return the (generic) type of an instance. E.g. a list of ints will give List[int].
common_ancestor Return the closest common ancestor of the given instances.
common_ancestor_of_types Return the closest common ancestor of the given classes.

Types

Type Description
T A generic Type var.
KT A Type var for keys in a dict.
VT A type var for values in a dict.
Empty The type of emptiness (= Parameter.empty).
Unknown The type of something unknown.
Module The type of a module.
NoneType The type of None.

SubscriptableType

This metaclass allows a type to become subscriptable.

Example:

class MyClass(metaclass=SubscriptableType):
    ...

Now you can do:

MyClass2 = MyClass['some args']
print(MyClass2.__args__)
print(MyClass2.__origin__)

Output:

some args
<class '__main__.MyClass'>

Something

Define an interface with typish.Something.

Example:

Duck = Something['walk': Callable[[], None], 
                 'quack': Callable[[], None]]

Anything that has the attributes defined in Something with the right type is considered an instance of that Something (classes, objects, even modules...).

The builtin isinstance is supported as well as typish.instance_of.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for typish, version 1.2.0
Filename, size File type Python version Upload date Hashes
Filename, size typish-1.2.0-py3-none-any.whl (9.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page