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

mypy should require the optional type to be made explicit. #12878

Closed
vivodi opened this issue May 27, 2022 · 1 comment
Closed

mypy should require the optional type to be made explicit. #12878

vivodi opened this issue May 27, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@vivodi
Copy link

vivodi commented May 27, 2022

Bug Report

Quoted from PEP 484:

A past version of this PEP allowed type checkers to assume an optional type when the default value is None, as in this code:

def handle_employee(e: Employee = None): ...

This would have been treated as equivalent to:

def handle_employee(e: Optional[Employee] = None) -> None: ...

This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.

To Reproduce

  1. input the code snippet:
    def handle_employee(e: Employee = None): ...
  2. run mypy handle_employee.py

Expected Behavior

mypy show a warning or an error like this:

error: Incompatible default for argument "e" (default has type "None", argument has type "Employee")

Actual Behavior

No warning or error is given.

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: no flag
  • Mypy configuration options from mypy.ini (and other config files): I don't set it.
  • Python version used: 3.10.4
  • Operating system and version: Windows 11
@vivodi vivodi added the bug mypy got something wrong label May 27, 2022
@AlexWaygood
Copy link
Member

Duplicate of #9091

@AlexWaygood AlexWaygood marked this as a duplicate of #9091 May 27, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 27, 2022
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

2 participants