Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create my own "parameterized" type in Python (like `Optional[T]`)?

I want to create my own parameterized type in Python for use in type hinting:

class MaybeWrapped:
    # magic goes here

T = TypeVar('T')

assert MaybeWrapped[T] == Union[T, Tuple[T]]

Never mind the contrived example; how can I implement this? I looked at the source for Union and Optional, but it looks like some fairly low-level hackery that I'd like to avoid.

The only suggestion in the documentation comes from an example re-implementation of Mapping[KT,VT] that inherits from Generic. But that example is more about the __getitem__ method than about the class itself.

like image 813
shadowtalker Avatar asked Sep 23 '17 17:09

shadowtalker


People also ask

What is optional in Python typing?

Directly from mypy typing module docs. Optional[str] is just a shorthand or alias for Union[str, None] . It exists mostly as a convenience to help function signatures look a little cleaner. Update for Python 3.10+ you can now use the pipe operator as well.

How do you create a new type in Python?

The short answer is you can't make a new type in python without editing the source code (written in C). However the answer about creating a class in python is probably the easier route to go since editing the source can create compatibility problems with packages (potentially speaking).

How do you define a type in Python?

The type can be also called using the syntax: type(name, bases, dict). The three parameters passed to type()i.e., name, bases and dict are the components that make up a class definition. The name represents the class name, the bases is the base class, and dict is the dictionary of base class attributes.


1 Answers

If you're just trying to create generic classes or functions, try taking a look at the documentation on mypy-lang.org about generic types -- it's fairly comprehensive, and more detailed then the standard library typing docs.

If you're trying to implement your specific example, it's worth pointing out that type aliases work with typevars -- you can simply do:

from typing import Union, TypeVar, Tuple

T = TypeVar('T')

MaybeWrapped = Union[T, Tuple[T]]

def foo(x: int) -> MaybeWrapped[str]:
    if x % 2 == 0:
        return "hi"
    else:
        return ("bye",)

# When running mypy, the output of this line is:
# test.py:13: error: Revealed type is 'Union[builtins.str, Tuple[builtins.str]]'
reveal_type(foo(3))

However, if you're trying to construct a generic type with genuinely new semantics, you're very likely out of luck. Your remaining options are to:

  1. Construct some kind of custom class/metaclass thing that PEP 484-compliant type checkers can understand and use that.
  2. Modify the type checker you're using somehow (mypy has an experimental "plugin" system, for example)
  3. Petition to modify PEP 484 to include your new, custom type (you can do this by opening an issue in the typing module repo).
like image 181
Michael0x2a Avatar answered Sep 26 '22 08:09

Michael0x2a