Fix property access for function types - #21885
Open
daleselaji-dev wants to merge 3 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.
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
Member analysis can apply descriptor access a second time after a property getter has already produced its value. The original fix skipped that duplicate pass for read-only properties, but the first review repair also exposed an existing dataclass-transform descriptor case whose synthetic read-only field still needs descriptor access.
Solution
Skip the duplicate descriptor pass for ordinary read-only property results while preserving descriptor evaluation for fields recorded in the dataclass-transform metadata. This keeps the reported
FunctionTypeproperty narrowing correct without regressing generic descriptor-backed dataclass fields.Changes
analyze_var.FunctionType/MethodTyperegression and descriptor fixture.check-dataclass-transform.test.Testing
wrapped.__func__astypes.MethodTypeand emitted two[unreachable]errors.pytest -n0 mypy/test/testcheck.py -k 'testPropertyReturningFunctionType or testDataclassTransformGenericDescriptor'— 3 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 --check: passed.32691141642failed consistently on the pre-existingcheck-dataclass-transform.testexpectation after the original diff; the new same-scope head is3f4c859and the local regression is green.Compatibility/Risk
Only static member-type analysis is changed. Ordinary read-only properties retain value semantics, settable properties retain their descriptor behavior, and synthetic dataclass-transform descriptor fields retain instance access through
__get__. Runtime Python behavior is unchanged.Notes for Reviewer
The added metadata check is intentionally narrow: it identifies a dataclass field by the existing
info.metadata["dataclass"]["attributes"]record instead of broadening descriptor handling for every property. This contribution was AI-assisted.Linked Issue
Fixes #21879