[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2020-03-08 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

PR 14166 does not fix this issue.

--
nosy: +serhiy.storchaka

___
Python tracker 

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



[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2019-11-17 Thread Ivan Levkivskyi


Ivan Levkivskyi  added the comment:

> I'm not sure what can be done with this. The problem is that the decorator 
> doesn't know what's in the caller's namespace. The type being added is 
> "typing.Any". If the caller doesn't import typing, then get_type_hints will 
> fail (as demonstrated here).

IIUC the main problem is that get_type_hints() fails even if typing is 
imported. I would expect this to work (just repeating the original example in a 
more compact form):

import dataclasses
import typing
A = dataclasses.make_dataclass('A', ['a_var'])
typing.get_type_hints(A)  # This currently crashes

Interestingly, if I use a very similar call that it works:

>>> typing.get_type_hints(A, globalns=globals())
{'a_var': typing.Any}

So the core of the issue is that the globals are identified incorrectly, and 
indeed if I look at the generated class it looks wrong:

>>> A.__module__
'types'  # Should be '__main__'

I think we should fix the ``__module__`` attribute of the dynamically generated 
dataclasses (for example the way it is done for named tuples).

Btw, https://github.com/python/cpython/pull/14166 may potentially fix the 
``__module__`` attribute here too.

--

___
Python tracker 

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



[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2019-10-28 Thread Eric V. Smith


Eric V. Smith  added the comment:

I'm not sure what can be done with this. The problem is that the decorator 
doesn't know what's in the caller's namespace. The type being added is 
"typing.Any". If the caller doesn't import typing, then get_type_hints will 
fail (as demonstrated here).

The only thing I can think of is using a type that's in builtins. "object" 
springs to mine, but of course that's semantically incorrect.

Or, maybe I could use "dataclasses.sys.modules['typing'].Any". I don't 
currently import sys (I don't think), but this should be a cheap import. Then 
if typing.get_type_hints() is called, we know typing will have already been 
importing.

But what if "dataclasses" isn't in the caller's namespace? I guess if I could 
find some way to navigate to sys.modules from __builtins__, that would largely 
work, absent playing games with builtins.

--

___
Python tracker 

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



[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2019-09-02 Thread Ivan Levkivskyi


Ivan Levkivskyi  added the comment:

It looks like https://github.com/python/cpython/pull/9518 will fix also this 
one.

--
nosy: +levkivskyi

___
Python tracker 

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



[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2019-08-26 Thread Eric V. Smith


Change by Eric V. Smith :


--
assignee:  -> eric.smith

___
Python tracker 

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



[issue37948] get_type_hints fails if there are un-annotated fields in a dataclass

2019-08-26 Thread Arne Recknagel


New submission from Arne Recknagel :

When declaring a dataclass with make_dataclass, it is valid to omit type 
information for fields. __annotations__ understands it and just adds 
typing.Any, but typing.get_type_hints fails with a cryptic error message:

>>> import dataclasses
>>> import typing
>>> A = dataclasses.make_dataclass('A', ['a_var'])
>>> A.__annotations__
{'a_var': 'typing.Any'}
>>> typing.get_type_hints(A)
Traceback (most recent call last):
  File "", line 1, in 
  File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 973, in 
get_type_hints
value = _eval_type(value, base_globals, localns)
  File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 260, in _eval_type
return t._evaluate(globalns, localns)
  File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 464, in _evaluate
eval(self.__forward_code__, globalns, localns),
  File "", line 1, in 
NameError: name 'typing' is not defined


Adding typing.Any explicitly is an obvious workaround:

>>> B = dataclasses.make_dataclass('B', [('a_var', typing.Any)])
>>> typing.get_type_hints(B)
{'a_var': typing.Any}

There is already a bug filed regarding datalcasses and get_type_hints which 
might be related: https://bugs.python.org/issue34776

--
messages: 350488
nosy: arne, eric.smith
priority: normal
severity: normal
status: open
title: get_type_hints fails if there are un-annotated fields in a dataclass
type: behavior
versions: Python 3.7, Python 3.8

___
Python tracker 

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