Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3.5.[01]: Union[Pattern, str] raises an error #6

Closed
asottile opened this issue Aug 24, 2019 · 6 comments · Fixed by #9
Closed

python3.5.[01]: Union[Pattern, str] raises an error #6

asottile opened this issue Aug 24, 2019 · 6 comments · Fixed by #9
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@asottile
Copy link
Owner

Need to make sure this gets quoted instead of left unquoted:

>>> Union[AnyStr, str]
typing.Union[~AnyStr, str]
>>> Union[str, Pattern]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/typing.py", line 534, in __getitem__
    dict(self.__dict__), parameters, _root=True)
  File "/usr/local/lib/python3.5/typing.py", line 491, in __new__
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/usr/local/lib/python3.5/typing.py", line 491, in <genexpr>
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
TypeError: issubclass() arg 1 must be a class
@asottile asottile added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Aug 24, 2019
@asottile
Copy link
Owner Author

The hint is to add a visit_Subscript to here:

class Visitor(ast.NodeVisitor):

And then report the error in the flake8 plugin below

Probably need to check for Pattern exactly, there may be other types that have problems as well

The nice thing about suggesting quoting is it won't show up in the ast because they're just strings!

@atugushev
Copy link
Contributor

Seems like only affects Pattern and Match.

Probably need to check for Pattern exactly, there may be other types that have problems as well

Should typing.Pattern (as ast.Attribute, not ast.Name) be checked as well? For cases like:

def foo(bar: typings.Union[typings.Pattern, str])

@asottile
Copy link
Owner Author

asottile commented Nov 5, 2019

yeah probably handle both typing.Pattern and Pattern -- the visitor already records the from import. if you're looking for prior art, pyupgrade does similar things for six imports

@atugushev
Copy link
Contributor

Thanks for the hint!

@atugushev
Copy link
Contributor

atugushev commented Nov 8, 2019

@asottile

Could you help me with an error text? I came up with:

  1. TYP003 Union[Pattern, ...] or Union[Match, ...] is broken in <3.5.2
  2. TYP003 Union[Pattern, Match, ...] is broken in <3.5.2
  3. TYP003 Union[Pattern, Match, ...] is broken in <3.5.2, consider quoting type annotation
  4. TYP003 Union[Match, ...] or Union[Pattern, ...] must be quoted in <3.5.2

@asottile
Copy link
Owner Author

asottile commented Nov 8, 2019

available in 1.5.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants