Skip to content

Complex annotation expressions yield errors #554

Closed

Description

@huettenhain

Consider the following code:

class Annotation:
    def __init__(self, *args, **kwargs):
        pass


def test(a: Annotation('Yikes', 'This is fine.', foo=NotImplemented, bar=12)):
    return a


print(test(12))

Running this with Python 3.8.3 works fine, and it is also valid code according to the PEP, quote:

Function annotations are nothing more than a way of associating arbitrary Python expressions with various parts of a function at compile-time.

However, running Pyflakes 2.2.0 produces the following output:

$ pyflakes test.py
test.py:6:24 undefined name 'Yikes'
test.py:6:33 syntax error in forward annotation 'This is fine.'

Activity

  1. asottile commented on Jun 6, 2020

    @asottile
    Member

    PEP 593 is not yet implemented (it was not accepted / ready when pyflakes 2.2 was released) but we would be happy to accept a patch to improve the situation there. there's similar specific handling for Literal and cast already that you may find helpful for implementing this

  2. asottile commented on Jun 6, 2020

    @asottile
    Member

    oh actually this is separate, your code isn't valid under PEP 484:

    $ mypy t.py 
    t.py:6: error: Invalid type comment or annotation
    t.py:6: note: Suggestion: use Annotation[...] instead of Annotation(...)
    Found 1 error in 1 file (checked 1 source file)
  3. asottile commented on Jun 6, 2020

    @asottile
    Member

    (484 has superseded 3107 in case that's not clear from above)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions