python / python/mypy

Fails to flag `None` passed to constructor of `enum.Enum`-based classes

Open
#19,094 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-enum
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy fails to flag None passed to constructor of enum.Enum-based classes.

To Reproduce

import enum


class Foo(enum.Enum):
    one = 1


Foo(None)

Note that changing to class Foo(int, enum.Enum) does make mypy flag the error. However, the two aren't equivalent as the latter lets you do Foo.one + 1 while the former requires Foo.one.value + 1.

Edit: In fact, in both forms it fails to flag invalid arguments such as Foo(2). I do realize this is a slightly different case, which is why I'm focusing on None which has different behavior between the two.

Expected Behavior

I would expect mypy to flag the last line as an error:

error: No overload variant of "Foo" matches argument type "None"  [call-overload]

Actual Behavior

Mypy reports no errors.

Your Environment

Tested on

  • Mypy 1.13.0 and 1.15.0
  • CPython 3.11.10

Used mypy --strict file.py and just mypy file.py.

Contents of mypy.ini (I actually use a pyproject.toml file, but have tested with only this ini-file):

[mypy]
check_untyped_defs = true
disallow_any_generics = false
disallow_incomplete_defs = false
disallow_subclassing_any = false
disallow_untyped_calls = false
disallow_untyped_decorators = true
disallow_untyped_defs = false
enable_error_code = truthy-bool, truthy-iterable
extra_checks = true
ignore_missing_imports = false
local_partial_types = true
no_implicit_optional = true
no_implicit_reexport = false
no_namespace_packages = true
python_version = 3.11
strict_equality = true
warn_redundant_casts = true
warn_return_any = false
warn_unused_configs = true
warn_unused_ignores = true

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided enum.Enum reproducer and compare it with the int, enum.Enum case that already reports an error. Trace how mypy checks constructor calls for enum-based classes, then add coverage for Foo(None) and invalid values such as Foo(2); done means the expected call-overload error is reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.