python / python/mypy

False positive with strict_optional disabled just in submodule

Open
#15,914 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

The simplest example I got is the below setup, which I also attach here for convenience.

Directory structure:

.
├── my_subdir
│   ├── dog.py
│   └── __init__.py
├── pyproject.toml
└── repro.py

and the files are:

[tool.poetry.dependencies]
python = "^3.11.2"


[tool.poetry.dev-dependencies]
mypy = "1.5.1"


[tool.mypy]
strict_optional = true


[[tool.mypy.overrides]]
module = "my_subdir.*"
strict_optional = false
# dog.py
from dataclasses import dataclass

@dataclass
class Dog:
   height: float | None
# repro.py
from my_subdir.dog import Dog

height: float | None = 1.0
Dog(height)

Now, enter command: mypy repro.py.

Expected Behavior

No errors

Actual Behavior

repro.py:4: error: Argument 1 to "Dog" has incompatible type "float | None"; expected "float"  [arg-type]

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: _none)
  • Python version used: 3.11.4

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 by running mypy repro.py with the reported pyproject.toml, my_subdir/dog.py, and repro.py setup. Trace how the tool.mypy.overrides setting for my_subdir.* is applied when checking the imported Dog constructor. Done means the command reports no incompatible-type error for the nullable height argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.