Python 3.5+
Since Python 3.5 you may use typing module.
Quoting docs, A type alias is defined by assigning the type to the alias:
Copy# Python 3.5-3.8
from typing import List
Vector = List[float]
# Python 3.9+
Vector = list[float] # No import needed, lower case l
To learn more about enforcing types in Python you may want to get familiar with PEPs: PEP483 and PEP484.
Python historically was using duck-typing instead of strong typing and hadn't built-in way of declaring types before 3.5 release.
Answer from Łukasz Rogalski on Stack OverflowVideos
Python 3.5+
Since Python 3.5 you may use typing module.
Quoting docs, A type alias is defined by assigning the type to the alias:
Copy# Python 3.5-3.8
from typing import List
Vector = List[float]
# Python 3.9+
Vector = list[float] # No import needed, lower case l
To learn more about enforcing types in Python you may want to get familiar with PEPs: PEP483 and PEP484.
Python historically was using duck-typing instead of strong typing and hadn't built-in way of declaring types before 3.5 release.
Python 3.10+
Since Python 3.10, the TypeAlias annotation is available in the typing module.
It is used to explicitly indicate that the assignment is done to generate a type alias. For example:
CopyPoint: TypeAlias = tuple[float, float]
Triangle: TypeAlias = tuple[Point, Point, Point]
You can read more about the TypeAlias annotation on the PEP 613 that introduced it.
I have a Python library which has (in my development branch on git) recently had a facelift. It has bindings to Rust, and due to a lot of mostly aesthetic reasons, I now have two versions of every class, a version A and a version B. For instance, if I had
class ClassA:
pass # lots of stuff hereI now have
class ClassB:
pass # lots of stuff here
class ClassC:
pass # similar stuffI’d like to maintain some form of backward compatibility, and ClassB is essentially the same as ClassA so I was thinking I could either do:
ClassA = ClassB
# or
class ClassA(ClassB):
passI’m not sure if there’s much of a difference in how those work really, although I guess the second implies an inheritance structure. The issue I’m having is with type hints. Since ClassA and ClassB are written in Rust, the only way to explicitly write type hints is through a __init__.pyi file. Now I have to write separate hints for ClassB and ClassC because they only work with the B or C variants of other classes, but I’m struggling with a way to write the hints for ClassA. Particularly, I could write identical stubs for As and Bs, and write union types for every time a method takes or returns one of these, but this would take a lot of time and result in a lot of duplicate code. Is there a way to avoid this?
Note that I am not looking for a type alias (as far as I know).
Edit: I gave up too quickly, I think I’ve answered my own question. I now only have type hints for B and C versions, and under the B type hint I just have the same ClassA = ClassB which I have in the __init__.py file (it has to be after the type hint because those files really are read top to bottom).
Hi
Im reading through the typing documentation https://docs.python.org/3/library/typing.html and have a question that I cannot answer.
In the past when I wanted to use type aliases I would use code like Vector = list[float] (I think that I must have picked this up from a post on stack overflow or something).
However, in the document above it suggests using the code type Vector = list[float].
The difference between the two is that the data type is types.GenericAlias (the list[float]) for the first Vector and typing.TypeAliasType for the second Vector.
But besides that I am not really sure what is the difference between these two methods. Im not sure where the reason to use one over the other is. Im also not sure where the documntation is for the first example (maybe technically this is not a Type Alias).
Im not sure if anyone can help here?