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

Different behavior between Union and | in mypy when dealing with optionals #10499

Closed
jevandezande opened this issue May 18, 2021 · 2 comments
Closed
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)

Comments

@jevandezande
Copy link

In the following two code examples, the first gives an error, while the latter does not. As I understand type1 | type2 is supposed to be an alias for Union[type1, type2] in python3.10 (hence the from __future__ import annotations).

from __future__ import annotations

def f(a: int | str = None):
    pass

error: Incompatible default for argument "a" (default has type "None", argument has type "Union[int, str]")

from typing import Union

def f(a: Union[int, str] = None):
    pass

As of PEP 484, non-explicit optional is discouraged. Is this the correct behavior, or a bug?

Your Environment

  • python = 3.9.1
  • mypy = 0.812
@jevandezande jevandezande added the bug mypy got something wrong label May 18, 2021
@JelleZijlstra
Copy link
Member

This is related to the --no-implicit-optional flag. We're already planning to flip the default for this flag (#9091), so maybe it's a good thing that the special casing doesn't apply to the new | operator.

@AlexWaygood AlexWaygood added the topic-pep-604 PEP 604 (union | operator) label Mar 31, 2022
@hauntsaninja
Copy link
Collaborator

It wasn't intentional that implicit optionals don't work with PEP 604 syntax, but I'm not going to add the special casing for it. As you say, PEP 484 discourages this, it's not really supported by other type checkers, and the next release of mypy flips the default for --no-implicit-optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator)
Projects
None yet
Development

No branches or pull requests

4 participants