Skip to content

ClassVar as string fails when getting type hints #505

@euresti

Description

@euresti

I know there are plans to make all type annotations strings eventually. So I was playing around and discovered this problem.

import typing

class Good:
    foo: typing.ClassVar[int] = 7
print(typing.get_type_hints(Good))

class Bad:
    foo: 'typing.ClassVar[int]' = 7
typing.get_type_hints(Bad)
{'foo': typing.ClassVar[int]}
Traceback (most recent call last):
  File "classvar_problem.py", line 10, in <module>
    typing.get_type_hints(Bad)
  File "/usr/local/homebrew/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 1511, in get_type_hints
    value = _eval_type(value, base_globals, localns)
  File "/usr/local/homebrew/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 350, in _eval_type
    return t._eval_type(globalns, localns)
  File "/usr/local/homebrew/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 246, in _eval_type
    "Forward references must evaluate to types.")
  File "/usr/local/homebrew/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/typing.py", line 374, in _type_check
    raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Forward references must evaluate to types. Got typing.ClassVar[int].

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions