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

Function default arguments false negative #12869

Closed
xmatthias opened this issue May 25, 2022 · 3 comments
Closed

Function default arguments false negative #12869

xmatthias opened this issue May 25, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@xmatthias
Copy link

Bug Report

Type-checking a method with a typed argument (typed to any regular type) with None as default yields no error.

I would expect this to be a type-error - as the real type of other in the below case is Optional[str].

To Reproduce

def random_function(value: str, other: str = None):
        pass

Playground:
https://mypy-play.net/?mypy=master&python=3.10&gist=c9a5da3dab27e76281498b25ca3a5e4d

Expected Behavior
Raising problematic default value, causing either a change in default, or a re-typing to Optional[str].

Actual Behavior

No error from mypy

Interestingly, pyright does properly detect this and raises the following error (this is how i discovered this bug).

error: Expression of type "None" cannot be assigned to parameter of type "str"
    Type "None" cannot be assigned to type "str" (reportGeneralTypeIssues)

Your Environment

  • Mypy version used: 0.950
  • Mypy command-line flags: /
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.10
@xmatthias xmatthias added the bug mypy got something wrong label May 25, 2022
@erictraut
Copy link

The original PEP 484 indicated that when a parameter had a None default argument, it should be interpreted implicitly as though the declared type were unioned with None. The PEP was later revised, but it looks like mypy still defaults to this old behavior. If you use --strict mode, mypy does report this error.

@xmatthias
Copy link
Author

--no-implicit-optional would explicitly enable this behavior (--strict has "other" side-effects too).

I'm personally more in favor of the implicit behavior (mypy default) - but struggled to find the "proper" flag for it while opening the issue.
I've now also discovered pyrights strictParameterNoneValue - which allows to align the behavior of the 2 tools (although they have diverging defaults).

@JelleZijlstra
Copy link
Member

@erictraut is right. The specific flag for this behavior is --no-implicit-optional: https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-optional.

We already have issue #9091 about changing the default, so closing this as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants