Fix property access for function types - #21885
Open
daleselaji-dev wants to merge 2 commits into
Open
Conversation
daleselaji-dev
marked this pull request as ready for review
August 24, 2026 03:17
This comment has been minimized.
This comment has been minimized.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: colour (https://github.com/colour-science/colour)
- colour/continuous/signal.py:1207: error: Argument 1 to "as_float_array" has incompatible type "ndarray[tuple[int], dtype[Any]] | ExtensionArray"; expected "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" [arg-type]
+ colour/continuous/signal.py:1207: error: Argument 1 to "as_float_array" has incompatible type "ndarray[tuple[int], dtype[Any]] | ExtensionArray | Categorical[object]"; expected "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" [arg-type]
freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/freqai/freqai_interface.py:950: error: Argument 1 to "to_datetime" has incompatible type "Any | object"; expected "float | str | datetime | datetime64[Any] | date" [arg-type]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Mypy treats a property returning
types.FunctionTypeas a bound method when the value is accessed from an instance. A validisinstance(..., FunctionType)assertion is therefore reported as an impossible intersection withMethodTypewhen--warn-unreachableis enabled.Root Cause
After resolving a property getter, member analysis applies descriptor access again to the getter's returned value. That second descriptor pass binds the returned
FunctionTypeas if it were the class attribute itself.Solution
Skip the post-property descriptor pass. Descriptor handling has already been applied to the property getter; the value returned by a property must be treated as the value, even when that value implements the descriptor protocol.
Changes
analyze_var.types.FunctionType/MethodTypefixture.Testing
wrapped.__func__astypes.MethodTypeand emitted two[unreachable]errors.pytest -n0 mypy/test/testcheck.py -k testPropertyReturningFunctionType— 1 passed.check-classes.test: 610 passed, 1 existing xfailed.python -m mypy --config-file mypy_self_check.ini -p mypy— 196 source files, no issues.git diff --checkpassed.Compatibility/Risk
Only property access is changed. Methods, ordinary descriptors, and direct class-attribute descriptor access retain their existing analysis. The fix prevents a false-positive narrowing error without changing runtime semantics.
Notes for Reviewer
The test specifically models the reported
FunctionType/MethodTypefinal-class intersection and verifies that the property result remainsFunctionType.Linked Issue
Fixes #21879