Skip to content
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 issue: Invalid combination of type qualifiers #17

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

Open issue: Invalid combination of type qualifiers #17

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 of an annotation, by stripping and keeping track of the type qualifiers used:

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

Depending on the annotation source (a class, a function parameter, etc), different type qualifiers are allowed. For classes, users can use Final or ClassVar:

class A:
    a: ClassVar[int]
    b: Final[str]

However, it seems like ClassVar and Final can't be used together. Code sample in pyright playground:

from typing import ClassVar, Final

class A:
    a: Final[ClassVar[int]]  # "ClassVar" is not allowed in this context
    b: ClassVar[Final[int]]  # "Final" is not allowed in this context

There is currently no logic to disallow this. Is it worth checking for this?

As a side note, combining ClassVar and Final is allowed for dataclasses (and types using @dataclass_transform).

@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