[issue43746] Weird typing annotation closure behavior

2021-04-09 Thread Guido van Rossum
Guido van Rossum added the comment: Just Don't Do This. -- resolution: -> wont fix stage: -> resolved status: open -> closed ___ Python tracker ___

[issue43746] Weird typing annotation closure behavior

2021-04-09 Thread conchylicultor
conchylicultor added the comment: Yes, I know I can rename the closure, or wrap the annotation in 'quote'. I just wanted to point this out as it felt confusing to me. For instance, it feels inconsistent with: ``` def fn(datetime: datetime.Time): # Works as expected ``` Or: ``` @dataclass

[issue43746] Weird typing annotation closure behavior

2021-04-09 Thread Joël Larose
Joël Larose added the comment: An easy workaround would be to alias your import or to import your class directly. ``` from ... import losses as l class A: losses: l.Losses = l.Losses() ``` or ``` from ...losses import Losses class A: losses: Losses = Losses() ``` -- nosy:

[issue43746] Weird typing annotation closure behavior

2021-04-09 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: I guess this is similar to the explanation at https://bugs.python.org/issue36363#msg338389 > The problem in the original code is that the annotation references a global > name that is shadowed by a local (to the class body) name, and because of

[issue43746] Weird typing annotation closure behavior

2021-04-09 Thread conchylicultor
conchylicultor added the comment: Sure, here is a minimal reproductible example demonstrating the issue: ``` import pathlib class C: pathlib: pathlib.Path = None ``` Which raises: ``` File "py", line 5, in C pathlib: pathlib.Path = None AttributeError: 'NoneType' object has

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread Eric V. Smith
Eric V. Smith added the comment: Can you put together an example we can run? Either from a single file, or multiple modules in the current directory? The "..." import makes it complicated to reproduce. -- ___ Python tracker

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread Eric V. Smith
Change by Eric V. Smith : -- Removed message: https://bugs.python.org/msg390548 ___ Python tracker ___ ___ Python-bugs-list mailing

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread Eric V. Smith
Eric V. Smith added the comment: Can you put together an example we can actually run? -- nosy: +eric.smith ___ Python tracker ___

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread conchylicultor
conchylicultor added the comment: The above example is a real world example I have currently have. Basically I have some dataclass based configuration like: in losses.py: ``` class LossesParams: ... ``` in dataset.py: ``` class DatasetParams: ... ``` in config.py: ```

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread Larry Hastings
Larry Hastings added the comment: By "use case", I mean, what problem are you solving in a useful program by doing this? So far it seems like a pointless exercise in seeing what funny behavior you can try with annotations. -- ___ Python tracker

[issue43746] Weird typing annotation closure behavior

2021-04-08 Thread conchylicultor
conchylicultor added the comment: > Do you have an actual use case for self-referential annotations? I'm not sure I understand the question. My use case is the following: ``` from ... import losses class A: losses: losses.Losses = losses.Losses() ``` Currently this is failing be cause

[issue43746] Weird typing annotation closure behavior

2021-04-06 Thread Larry Hastings
Larry Hastings added the comment: Do you have an actual use case for self-referential annotations? -- nosy: +larry ___ Python tracker ___

[issue43746] Weird typing annotation closure behavior

2021-04-06 Thread conchylicultor
conchylicultor added the comment: Interestingly mypy, pylint correctly resolve the closure. ``` class A: dataclasses: dataclasses.Field = dataclasses.field() A.dataclasses.other # mypy error: "Field[Any]" has no attribute "other" ``` So the current workaround is to use quotes: ```

[issue43746] Weird typing annotation closure behavior

2021-04-06 Thread conchylicultor
Change by conchylicultor : -- components: +Library (Lib) -Interpreter Core ___ Python tracker ___ ___ Python-bugs-list mailing list

[issue43746] Weird typing annotation closure behavior

2021-04-06 Thread conchylicultor
Change by conchylicultor : -- components: +Interpreter Core type: -> behavior versions: +Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9 ___ Python tracker ___

[issue43746] Weird typing annotation closure behavior

2021-04-06 Thread conchylicultor
New submission from conchylicultor : I observe some strange closure behavior for typing annotations when the name is defined ``` x: x = 1 # Works, __annotation__ == {'x': 1} ``` This creates issue, for example: ``` from ... import losses class A: # AttributeError: 'Losses' object has no