[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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
class A:
  datetime: datetime.Time = field(default_factory=datetime.Time.now)
```

The `datetime.Time.now` and the `datetime.Time` of the same statement refer to 
different objects.

This might be the expected behavior, but this is confusing nonetheless.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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: +joel.larose

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 the 
> initialization, the latter takes precedence.  (To see for yourself, use the 
> dis module to disassemble the code for Spam and Spaz.)

--
nosy: +gvanrossum, xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 no attribute 'Path'
```

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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:
```
@dataclasses.dataclass
class Params:
  losses: losses.LossesParams = dataclasses.field()
  dataset: dataset.DatasetParams = dataclasses.field()
```
I want to use params as:
```
param = Params()
param.datasets.batch_size = 123
```
However the above code fail at `dataset: dataset.DatasetParams = 
dataclasses.field()` due to the closure issue.

The example is simplified but this is a very concrete problem I encountered.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 this get resolved as:

```
class A:
  name: .Losses().Losses = .Losses()
```
Instead of what I want/expected:
```
class A:
  name: .Losses = .Losses()
```

I would expect that both "losses.Losses" on the left and right of the `=` refer 
to the outer module (`name: .Losses`), while currently it is 
resolved as `name: name.Losses`

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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:

```
class A:
  # Type correctly inferred and no closure runtime error
  losses: 'losses.Losses' = losses.Losses()
```

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 attribute 'Losses'
  losses: losses.Losses = losses.Losses()
```

--
messages: 390304
nosy: conchylicultor
priority: normal
severity: normal
status: open
title: Weird typing annotation closure behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com