Skip to content

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

Closed as not planned
@vivodi

Description

@vivodi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions