Skip to content

Open issue: (dis)allowing type qualifiers in PEP 695 type aliases #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Viicos opened this issue Feb 18, 2025 · 0 comments
Open

Open issue: (dis)allowing type qualifiers in PEP 695 type aliases #16

Viicos opened this issue Feb 18, 2025 · 0 comments
Labels
open issues Open issues to be discussed with other users of the runtime typing ecosystem

Comments

@Viicos
Copy link
Member

Viicos commented Feb 18, 2025

In the introspection module, the inspect_annotation() function can be used to unpack the underlying type expression:

>>> inspect_annotation(
...     Final[Annotated[ClassVar[Annotated[int, 'meta_1']], 'meta_2']],
...     annotation_source=AnnotationSource.CLASS,
... )
...
InspectedAnnotation(type=int, qualifiers={'class_var', 'final'}, metadata=['meta_1', 'meta_2'])

However, the passed annotation expression could be defined as:

type MyAlias = ClassVar[int]

@my_dataclass
class A:
    f: Annotated[MyAlias, "meta_1"]

Static type checkers raise an error on the MyAlias definition, as type qualifiers can only be used in certain contexts (in the case of ClassVar, it can only be used within a class annotation).

When passing Annotated[MyAlias, "meta_1"] to inspect_annotation() (and setting unpack_type_aliases to 'eager'), we currently don't error:

>>> inspect_annotation(
...     Annotated[MyAlias, "meta_1"],
...     annotation_source=AnnotationSource.CLASS,
...     unpack_type_aliases="eager",
... )
...
InspectedAnnotation(type=int, qualifiers={'class_var'}, metadata=['meta_1'])

Should we error whenever a type qualifier is used inside a PEP 695 type alias? Or provide an option to (dis)allow it/raise a warning?

@Viicos Viicos added the open issues Open issues to be discussed with other users of the runtime typing ecosystem label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open issues Open issues to be discussed with other users of the runtime typing ecosystem
Projects
None yet
Development

No branches or pull requests

1 participant