You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow Final attributes declared in class body if initialized in ctor #8982
There is currently a safety check around TypeVar-dependent Final attributes declared in the class body that I believe we can loosen when the variable is assigned in the ctor. This should work analogously to non-TypeVar-dependent final attributes declared in the class body, which also require an assignment in the ctor (otherwise they emit "Final name must be initialized with a value").
Are you reporting a bug, or opening a feature request? feature request
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
final.py: note: In class "WidgetHolder":
final.py:13:5: error: Final name declared in class body cannot depend on type variables [misc]
widget: Final[WidgetT]
^
In case anyone comes across this issue, the workaround is currently to not declare in the class body, but declare in the ctor with self.widget: Final[WidgetT] = widget instead. But just because a workaround exists doesn't mean we shouldn't try to fix this.
C# has this feature with a readonly keyword. It would be nice to have here. You can modify the field as much as you want within the constructor but after that it's considered const.
@Kangaroux yep that's the intent of Final as well, and for the most part it already works like const or readonly. This issue is more about an implementation restriction, where the declaration is not allowed to be generic even when the constructor initializes it with a compatible object.
Activity
Kangaroux commentedon Jun 10, 2020
C# has this feature with a readonly keyword. It would be nice to have here. You can modify the field as much as you want within the constructor but after that it's considered const.
llchan commentedon Jun 11, 2020
@Kangaroux yep that's the intent of
Finalas well, and for the most part it already works likeconstorreadonly. This issue is more about an implementation restriction, where the declaration is not allowed to be generic even when the constructor initializes it with a compatible object.dtoniolo commentedon Jan 19, 2023
This is issue is over two years old, but i've just came across the exact same problem.
Finalattributes using typevars in dataclasses #21637