This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing.NamedTuple does not deduce Optional[] from using None as default field value
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, levkivskyi, vlad
Priority: normal Keywords:

Created on 2018-03-14 13:25 by vlad, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg313819 - (view) Author: Vlad Shcherbina (vlad) * Date: 2018-03-14 13:25
from typing import *

def f(arg: str = None):
    pass
print(get_type_hints(f))

# {'arg': typing.Union[str, NoneType]}
# as expected


class T(NamedTuple):
    field: str = None
print(get_type_hints(T))

# {'field': <class 'str'>}
# but it should be
# {'field': typing.Union[str, NoneType]}
# for consistency
msg313820 - (view) Author: Vlad Shcherbina (vlad) * Date: 2018-03-14 13:28
If the maintainers agree that it's desirable to automatically deduce optionality, I'll implement it.
msg313828 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-03-14 16:13
This is actually an intended behaviour. Moreover, the implicit optionality of arguments that default to `None` is deprecated and will be removed in one of the future versions. (Note that since typing module is still provisional, this will likely happen without any additional notice.)
msg313835 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-03-14 18:46
This bug report can be closed.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77256
2018-03-14 19:03:02levkivskyisetstatus: open -> closed
2018-03-14 18:48:30levkivskyisetresolution: not a bug
stage: resolved
2018-03-14 18:46:11gvanrossumsetmessages: + msg313835
2018-03-14 16:13:14levkivskyisetmessages: + msg313828
2018-03-14 15:43:58ned.deilysetnosy: + gvanrossum, levkivskyi
2018-03-14 13:28:34vladsetmessages: + msg313820
2018-03-14 13:25:09vladcreate