[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Guido van Rossum


Guido van Rossum  added the comment:

A simpler example shows it has nothing to do with annotations -- it is simply 
behavior of the typing module.

>>> import typing
>>> typing.Optional[str]
typing.Union[str, NoneType]
>>> typing.Optional[None]

>>> 

I don't think there's a bug here, and I am closing this as "not a bug". 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.)

--
resolution:  -> not a bug
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



[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

class Spam:
bar: typing.Optional[bar] = str

class Spaz:
bar: typing.Optional[bar] = None

print(Spam.__annotations__)
print(Spaz.__annotations__)

{'bar': typing.Union[str, NoneType]}
{'bar': }

In Spam bar has str assigned to it and seems like in Spaz bar is assigned None 
and hence during annotation creation this evaluates to bar: 
typing.Optional[None] where None is the value of bar and in Spam.bar it's 
typing.Union[str, NoneType] instead.

--

___
Python tracker 

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



[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I am not sure this is a problem with dataclasses. dataclasses acts upon 
information from cls.__dict__.get('__annotations__', {}) at [0] and sets the 
type attribute for the field. Seems like using a valid identifier (class or 
function) used as class attribute along with defining it as optional type of 
the same name sets None type in annotation. Also happens when a class attribute 
is used with optional type for another attribute as in Spam class This more 
feels like a behavior with typing module. I am adding typing module maintainers 
for clarification.

# bpo36363.py

import typing

class Baz:
pass

class Bar:
pass

class Foo:
baz: typing.Optional[Bar] = None
Bar: typing.Optional[Bar] = None

class Spam:
bar: typing.Optional[Bar] = None
baz: typing.Optional[bar] = None

print(Foo.__dict__.get('__annotations__', {}))
print(Spam.__dict__.get('__annotations__', {}))

$ ./python.exe ../backups/bpo36363.py
{'baz': typing.Union[__main__.Bar, NoneType], 'Bar': }
{'bar': typing.Union[__main__.Bar, NoneType], 'baz': }


[0] 
https://github.com/python/cpython/blob/dcf617152e1d4c4a5e7965733928858a9c0936ca/Lib/dataclasses.py#L828

--
nosy: +gvanrossum, levkivskyi, xtreak
versions: +Python 3.8

___
Python tracker 

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



[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee:  -> eric.smith

___
Python tracker 

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



[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +eric.smith

___
Python tracker 

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



[issue36363] Wrong type when missname dataclass attribute with existing variable name

2019-03-19 Thread Bastien Sevajol


New submission from Bastien Sevajol :

Hello,

For following code:

```
import dataclasses
import typing
from datetime import datetime


@dataclasses.dataclass
class Foo:
datetime: typing.Optional[datetime] = None


print(dataclasses.fields(Foo)[0].type)
```

`datetime` `Foo` attribute have `NoneType` type. Problem come from `datetime` 
attribute name is already used by the `from datetime import datetime` import. 
I'm not sure if it is a bug or a strange behavior but it seems not regular. 
Tested on python 3.8.0a2 with same result.

--
components: Extension Modules
messages: 338354
nosy: Bastien Sevajol
priority: normal
severity: normal
status: open
title: Wrong type when missname dataclass attribute with existing variable name
type: behavior
versions: Python 3.7

___
Python tracker 

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