Skip to content

mypy strict mode#9410

Open
jorenham wants to merge 4 commits intopython-pillow:mainfrom
jorenham:typing/strict
Open

mypy strict mode#9410
jorenham wants to merge 4 commits intopython-pillow:mainfrom
jorenham:typing/strict

Conversation

@jorenham
Copy link
Copy Markdown
Contributor

@jorenham jorenham commented Jan 27, 2026

This configures mypy to run in "strict mode", which according to mypy --help enables the following flags:

--warn-unused-configs, --disallow-any-generics, --disallow-subclassing-any, --disallow-untyped-calls, --disallow-untyped-defs, --disallow-incomplete-defs, --check-untyped-defs, --disallow-untyped-decorators, --warn-redundant-casts, --warn-unused-ignores, --warn-return-any, --no-implicit-reexport, --strict-equality,
--strict-bytes, --extra-checks

Enabling this caused a bunch of typing errors to surface, which are now also fixed.

Most of these are related to bytes/bytearray/memoryview not being assignable to each other. So I used their common denominator, collections.abc.Buffer, or rather its backport from typing_extensions (which is always available if TYPE_CHECKING because typeshed conveniently lies that it's part of the standard library).

Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
disallow_untyped_calls = false
enable_error_code = "ignore-without-code"
extra_checks = true
follow_imports = "silent"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything at https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-strict that mentions follow_imports? What was the thinking behind changing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, follow_imports is unrelated to --strict. It's just unnecessary to suppress those import errors.

@radarhere
Copy link
Copy Markdown
Member

The changes here should be re-evaluated after #9414's debate about Python versions is resolved.

@jorenham
Copy link
Copy Markdown
Contributor Author

Anything I can do to help with that?

@radarhere
Copy link
Copy Markdown
Member

#9414 (comment)

Is there authoritative guidance on best practices here?

The only thing I can think of is to ask someone at mypy for their official view on whether we need to run mypy against the oldest supported Python, newest Python, or everything in between.

@jorenham
Copy link
Copy Markdown
Contributor Author

#9414 (comment)

Is there authoritative guidance on best practices here?

The only thing I can think of is to ask someone at mypy for their official view on whether we need to run mypy against the oldest supported Python, newest Python, or everything in between.

@JukkaL sorry to bother you, but could I redirect this question to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants