This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Dataclass transform should ignore TypeAlias variables
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, GBeauregard, JelleZijlstra, eric.smith, thomkeh
Priority: normal Keywords:

Created on 2022-03-30 14:49 by thomkeh, last changed 2022-04-11 14:59 by admin.

Messages (5)
msg416368 - (view) Author: Thomas MK (thomkeh) Date: 2022-03-30 14:49
The dataclass transformation ignores attributes that are annotated as ClassVar. I think it should also ignore attributes that are annotated as TypeAlias.

Specifically, I have this usecase in mind:


class RunMode(Enum):
    release = auto()
    debug = auto()

@dataclass
class Run:
    Mode: TypeAlias = RunMode
    mode: Mode = Mode.release
msg416414 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-31 02:10
From a typing perspective this is reasonable. See this thread about type aliases in class scopes: https://mail.python.org/archives/list/typing-sig@python.org/thread/CGOO7GPPECGMLFDUDXSSXTRADI4BXYCS/ 

However, it's a niche use case and we could decide that we don't want to further complicate the annotation parsing in dataclasses.py.
msg416426 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2022-03-31 10:25
I think implementing this would add complexity to the code in dataclasses.py (though Eric's the expert!).

For your use case, is it essential that the type alias declaration be inside the class scope? Would it be possible for you to simply have the alias declaration in the global scope instead?
msg416428 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-31 10:50
Same question as Alex: what does the TypeAlias being inside the class offer that being at module level doesn't?
msg416903 - (view) Author: Thomas MK (thomkeh) Date: 2022-04-06 22:47
There is of course no hard reason for not using the global scope. I just often have enums (or other types) that are very closely linked to one class. And it makes sense to me then to have a TypeAlias in that class so that I don't have to import the enum separately.

For normal classes, the nested TypeAlias works completely fine in the type checkers I tested (pyright and mypy). It's just dataclasses that are the problem.

But I see now that there is a general wish to keep the implementation of dataclass simple, which I can understand.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91322
2022-04-06 22:47:22thomkehsetmessages: + msg416903
2022-03-31 10:50:45eric.smithsetmessages: + msg416428
2022-03-31 10:25:24AlexWaygoodsetmessages: + msg416426
2022-03-31 02:10:19JelleZijlstrasetmessages: + msg416414
2022-03-31 01:34:22JelleZijlstrasetnosy: + JelleZijlstra, AlexWaygood, GBeauregard
2022-03-31 00:32:52ned.deilysetnosy: + eric.smith
2022-03-30 14:49:32thomkehcreate