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.

Author kj
Recipients dpinol, gvanrossum, kj, levkivskyi
Date 2021-01-15.17:05:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610730335.05.0.319922550271.issue42377@roundup.psfhosted.org>
In-reply-to
Content
Sorry, I don't think this is a typing module issue.

The NameError stems from what you mentioned: 'A' not being defined since TYPE_CHECKING evaluates to false at runtime. This would raise an error in any Python code, not just typing. The equivalent is this::

>>> A
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined

Without the future import, def f(a: A): ... already throws that NameError. The reason why it works in your example is because 'A' isn't evaluated at function declaration time with the future import.

To appease your static type checker and not cause runtime errors, you might want to try:

f(cast("A", "anything"))

Which seems to work in mypy and pycharm. (Sorry, I don't have pyre/pytype/pyright to check with, though I would be surprised if this didn't pass on them.)

Please correct me if you feel anything I wrote was incorrect :).
History
Date User Action Args
2021-01-15 17:05:35kjsetrecipients: + kj, gvanrossum, levkivskyi, dpinol
2021-01-15 17:05:35kjsetmessageid: <1610730335.05.0.319922550271.issue42377@roundup.psfhosted.org>
2021-01-15 17:05:35kjlinkissue42377 messages
2021-01-15 17:05:34kjcreate