Skip to content

Can't typehint decorated functions in MatcherDecoratableTransformer with future annotations #870

@jakkdl

Description

@jakkdl

In flake8-trio we use from future import __annotations__ in order to use more modern typehinting when running python 3.9, but the typechecking that decorators do don't - so this leads to a crash on python3.9:

@m.visit(m.Await() | m.For(asynchronous=m.Asynchronous()))
def checkpoint_node(self, node: cst.Await | cst.For | cst.With):
    ...
  File "/home/h/Git/flake8-trio/flake8_trio/visitors/flake8triovisitor.py", line 172, in __init__
    super().__init__()
  File "/home/h/Git/flake8-trio/.tox/py39/lib/python3.9/site-packages/libcst/matchers/_visitors.py", line 483, in __init__
    _check_types(
  File "/home/h/Git/flake8-trio/.tox/py39/lib/python3.9/site-packages/libcst/matchers/_visitors.py", line 250, in _check_types
    _verify_return_annotation(
  File "/home/h/Git/flake8-trio/.tox/py39/lib/python3.9/site-packages/libcst/matchers/_visitors.py", line 123, in _verify_return_annotation
    type_hints = get_type_hints(meth)
  File "/usr/lib/python3.9/typing.py", line 1497, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/usr/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/usr/lib/python3.9/typing.py", line 554, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'ABCMeta' and 'ABCMeta'

I can workaround it by reverting to using Union, but I think it should be possible to add a future import to your eval environment?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmachineryInternal plumbing for visitor, transformer, matcher APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions