ClassVar should not be used outside of a variable declaration within a class. ClassVar is not an annotation that describes the type of a variable, but it describes a (non-type) attribute of a variable. Most other languages offer keywords for such attributes, but Python overloads the type annotation mechanism to handle these cases. Other examples include Final, InitVar, Required, and NotRequired. These special forms should not be used within type aliases because they are not type annotations.

The use of ClassVar should be flagged as an error when used in a type alias like this. When I implemented support for ClassVar in pyright, it never occurred to me that someone would attempt to use it in a type alias, so there is no check for this error condition. But you should assume that it will not work when used in this manner.

🌐
Python
typing.python.org › en › latest › spec › aliases.html
Type aliases — typing documentation
from typing import TypeAlias Url: TypeAlias = str def retry(url: Url, retry_count: int) -> None: ... Or by using the type statement (Python 3.12 and higher):
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
Or marked with TypeAlias to make it explicit that this is a type alias, not a normal variable assignment: from typing import TypeAlias Vector: TypeAlias = list[float]
🌐
Mypy
mypy.readthedocs.io › en › stable › kinds_of_types.html
Kinds of types - mypy 1.19.1 documentation
There is also use an older syntax for defining explicit type aliases, which was introduced in Python 3.10 (PEP 613): from typing import TypeAlias # "from typing_extensions" in Python 3.9 and earlier AliasType: TypeAlias = list[dict[tuple[int, str], set[int]]] | tuple[str, list[str]] Mypy recognizes named tuples and can type check code that defines or uses them.
🌐
Python
typing.python.org › en › latest › guides › modernizing.html
Modernizing Superseded Typing Features — typing documentation
from typing import TypeAlias # or typing_extensions.TypeAlias IntList: TypeAlias = list[int] ... Python 3.12 introduced new syntax for defining generic classes. Previously, generic classes had to derive from typing.Generic (or another generic class) and defined the type variable using ...
🌐
Reddit
reddit.com › r/learnpython › help understanding type aliases
r/learnpython on Reddit: Help understanding Type Aliases
April 28, 2025 -

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?

🌐
Mathspp
mathspp.com › blog › til › type-statement-and-type-aliases
TIL #084 – type statement and type aliases | mathspp
In 3.12, you could write it as such: ... In Python 3.11 and earlier, you'd have to quote the forward references or the self-reference of the first example, like so: from typing import TypeAlias LinkedList: TypeAlias = tuple[int, "LinkedList"] A: ...
🌐
GitHub
github.com › invoke-ai › InvokeAI › issues › 322
ImportError: cannot import name 'TypeAlias' from 'typing' on macOS · Issue #322 · invoke-ai/InvokeAI
June 18, 2022 - This was all working for me before pulling the latest version today, but I removed the ldm anaconda environment to be sure that everything was up to date and ran into problems, so I nuked everything and tried to install from scratch.
Published   Sep 02, 2022
🌐
Astral
docs.astral.sh › ruff › rules › type-alias-without-annotation
type-alias-without-annotation (PYI026) | Ruff
It's best to annotate type aliases with the typing.TypeAlias type to make it clear that the statement is a type alias declaration, as opposed to a normal variable assignment. ... Because this rule relies on the third-party typing_extensions module for Python versions before 3.10, its diagnostic will not be emitted, and no fix will be offered, if typing_extensions imports ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 76456913 › how-to-define-a-type-alias-typealias-that-is-evaluated-lazily-via-forwardref-i
typing - How to define a type alias (TypeAlias) that is evaluated lazily via ForwardRef in python? - Stack Overflow
# types.py -- a collection of our own type aliases from typing import TYPE_CHECKING, TypeAlias if TYPE_CHECKING: import tensorflow as tf MyTensorAlias = "tf.Tensor" # <-- now this is just a string assignment and will not evaluate to a `ForwardRef` # Notes of things that do not work: # 1) annotating with TypeAlias MyOtherTensorAlias: TypeAlias = "tf.Tensor" # <-- still just a str and will cause runtime errors # 2) Aliases behind `TYPE_CHECKING` if TYPE_CHECKING: MyLazyTensorAlias = tf.Tensor # <-- this is fine for static type checkers, but raises at runtime when `types has no member MyLazyTensorAlias` anymore.
🌐
Python.org
discuss.python.org › python help
TypeAlias cannot be indirectly imported? - Python Help - Discussions on Python.org
June 17, 2022 - Dear all, I received plenty of interesting comments regarding my previous question, so that I indulge myself in another one. In file a.py, you import TypeAlias from module typing from typing import TypeAlias In file b.py, you import all of module a and use TypeAlias from a import * t:TypeAlias = int Then Pyright raises an error: error: Expression of type "Type[int]" cannot be assigned to declared type "TypeAlias" "Type[type]" is incompatible with "Type[TypeAlias]" However, if I impor...
🌐
GitHub
github.com › alexdelorenzo › cast_control › issues › 16
cannot import name 'TypeAlias' from 'typing' Python3.8 fresh install · Issue #16 · alexdelorenzo/cast_control
October 27, 2021 - import adapters, base, types, server, mpris, interfaces File "/usr/local/lib/python3.8/dist-packages/mpris_server/adapters.py", line 5, in <module> from .base import URI, MIME_TYPES, PlayState, DEFAULT_RATE, Microseconds, \ File "/usr/local/lib/python3.8/dist-packages/mpris_server/base.py", line 10, in <module> from .types import TypedDict, TypeAlias, \ File "/usr/local/lib/python3.8/dist-packages/mpris_server/types.py", line 11, in <module> from typing_extensions import \ ImportError: cannot import name '_GenericAlias' from 'typing_extensions' (/usr/local/lib/python3.8/dist-packages/typing_extensions.py)
Published   Feb 15, 2022
🌐
Runebook.dev
runebook.dev › en › docs › python › library › typing › typing.TypeAlias
Beyond the Basics: A Friendly Guide to Python's Type Alias System
Forward References (Less Needed Now) Historically, it helped with types not yet defined, though modern Python (3.7+ with from __future__ import annotations, or Python 3.12+ with type statement) handles this better. ... from typing import TypeAlias, Dict, List # Type alias for a dictionary representing configuration settings ConfigDict: TypeAlias = Dict[str, str | List[str]] def load_config(path: str) -> ConfigDict: # ...
🌐
TestDriven.io
testdriven.io › tips › e2b4b882-eac8-4231-a64f-219e98b084a4
Tips and Tricks - Python type hints - creating a type alias | TestDriven.io
You can create a type alias to make your code more readable. from typing import List Vector = List[float] def scale(scalar: float, vector: Vector) -> Vector: return [scalar * num for num in vector]
🌐
Hacker News
news.ycombinator.com › item
I ran into: ImportError: cannot import name 'TypeAlias' from 'typing' (/opt/home... | Hacker News
September 3, 2022 - ImportError: cannot import name 'TypeAlias' from 'typing' (/opt/homebrew/Caskroom/miniconda/base/envs/ldm/lib/python3.9/typing.py) · stable-diffusion/src/k-diffusion/k_diffusion/sampling.py
🌐
Towards Data Science
towardsdatascience.com › home › latest › python type hinting: from type aliases to type variables and new types
Python Type Hinting: From Type Aliases To Type Variables and New Types | Towards Data Science
January 22, 2025 - This is how you would proceed when working on your project in which the number of types is limited. When working on a framework to be used by others, however, you should create more types. ... from typing import NewType Mm = NewType("Mm", float) Cm = NewType("Cm", float) M = NewType("M", float) Km = NewType("Km", float)
🌐
GitHub
gist.github.com › simonwhitaker › 3a24d669b26b4f373690c6eb113c3ae7
python-type-alias-vs-newtype.md · GitHub
Let's rewrite the code, but this time declare Point2D and Size2D as new types. from typing import NewType Point2D = NewType("Point2D", tuple[float, float]) Size2D = NewType("Size2D", tuple[float, float]) Rectangle = tuple[Point2D, Size2D] def get_area(rect: Rectangle) -> float: _, size = rect width, height = size return width * height # To instantiate types declared with NewType with # literal values, wrap the values in TypeName(...) origin = Point2D((0.0, 0.0)) size = Size2D((3.0, 4.0)) rect = (size, origin) print(get_area(rect))