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

Suggest using PEP 604-style union types #45

Closed
hoefling opened this issue Jun 16, 2021 · 4 comments · Fixed by #202
Closed

Suggest using PEP 604-style union types #45

hoefling opened this issue Jun 16, 2021 · 4 comments · Fixed by #202

Comments

@hoefling
Copy link
Contributor

hoefling commented Jun 16, 2021

Humble suggestion: add new code to suggest a replacement of Union[...] types with the | chain, e.g. Y093 Use PEP 604 union types. A simple implementation start could be e.g.

class PyVisitor(ast.NodeVisitor):
    ...
     def visit_FunctionDef(self, node):
        if node.returns.value is not None and node.returns.value.id in ("Optional", "Union"):
            self.error(node.returns, Y093)
        ...

    def visit_arguments(self, node):
        if arg.annotation.value.id in ("Optional", "Union"):
            self.error(arg, Y093)
        ...
@srittau
Copy link
Collaborator

srittau commented Aug 9, 2021

Another way to do this is to check Union and Optional imports. This would generate only one warning per file.

@AlexWaygood
Copy link
Collaborator

In my view, we should probably not bother with trying to port the PEP 604 logic in check_new_syntax to flake8-pyi for now. It would be non-trivial to do so, and hopefully, full mypy support for PEP 604 type aliases is just around the corner. Once it is, we can just ban importing Union and Optional, as @srittau suggests. That should be much easier.

@hoefling
Copy link
Contributor Author

hoefling commented Feb 2, 2022

Hmm, but I didn't propose building replacement snippets 8-) A simple Use PEP 604 union types would already suffice IMO.

@AlexWaygood
Copy link
Collaborator

Hmm, but I didn't propose building replacement snippets 8-) A simple Use PEP 604 union types would already suffice IMO.

Well, feel free to submit a PR, if you like ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants