Fix false positive on attribute access for TypeVar bounded by type#2672
Fix false positive on attribute access for TypeVar bounded by type#2672yangdanny97 wants to merge 1 commit intofacebook:mainfrom
type#2672Conversation
Summary: When a TypeVar has `bound=type`, pyrefly internally represents the bound as `type[Any]`, which resolves to `AttributeBase1::TypeAny` during attribute lookup. The quantified TypeVar attribute resolution only handled `AttributeBase1::ClassInstance`, causing any other variant (including `TypeAny`) to fall back to `object`. This produced false positives like "Object of class `object` has no attribute `__name__`" when accessing attributes that exist on the `type` class. The fix extracts the bound-to-ClassType mapping into a `quantified_bound_class` helper that handles both `ClassInstance` (the common case) and `TypeAny` (the `bound=type` case, mapping to `builtins.type`). This is applied to all four sites where quantified bounds are resolved: both `T` and `type[T]` paths, for both `Bound` and `Constraints` restrictions. Fixes facebook#2614 Differential Revision: D95419868
|
@yangdanny97 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95419868. |
|
Diff from mypy_primer, showing the effect of this PR on open source code: kornia (https://github.com/kornia/kornia)
- ERROR kornia/core/check.py:309:37-49: Object of class `object` has no attribute `__name__` [missing-attribute]
- ERROR kornia/core/check.py:309:96-106: Object of class `object` has no attribute `__name__` [missing-attribute]
pydantic (https://github.com/pydantic/pydantic)
- ERROR pydantic/config.py:1290:9-35: Object of class `object` has no attribute `__pydantic_config__` [missing-attribute]
+ ERROR pydantic/config.py:1290:9-35: Object of class `type` has no attribute `__pydantic_config__` [missing-attribute]
strawberry (https://github.com/strawberry-graphql/strawberry)
- ERROR strawberry/types/object_type.py:133:34-46: Object of class `object` has no attribute `__name__` [missing-attribute]
prefect (https://github.com/PrefectHQ/prefect)
- ERROR src/prefect/utilities/dispatch.py:40:25-32: Object of class `object` has no attribute `mro` [missing-attribute]
- ERROR src/prefect/utilities/dispatch.py:129:9-48: Object of class `object` has no attribute `__pydantic_init_subclass_original__` [missing-attribute]
+ ERROR src/prefect/utilities/dispatch.py:129:9-48: Object of class `type` has no attribute `__pydantic_init_subclass_original__` [missing-attribute]
- ERROR src/prefect/utilities/dispatch.py:132:9-39: Object of class `object` has no attribute `__pydantic_init_subclass__` [missing-attribute]
+ ERROR src/prefect/utilities/dispatch.py:132:9-39: Object of class `type` has no attribute `__pydantic_init_subclass__` [missing-attribute]
- ERROR src/prefect/utilities/dispatch.py:134:9-39: Object of class `object` has no attribute `__init_subclass_original__` [missing-attribute]
+ ERROR src/prefect/utilities/dispatch.py:134:9-39: Object of class `type` has no attribute `__init_subclass_original__` [missing-attribute]
- ERROR src/prefect/utilities/dispatch.py:163:45-52: Object of class `object` has no attribute `mro` [missing-attribute]
|
Primer Diff Classification✅ 3 improvement(s) | ➖ 1 neutral | 4 project(s) total 3 improvement(s) across kornia, strawberry, prefect.
Detailed analysis✅ Improvement (3)kornia (-2)
strawberry (-1)
prefect (+3, -5)
➖ Neutral (1)pydantic (+1, -1)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 heuristic, 3 LLM) |
grievejia
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
This pull request has been merged in e311fea. |
Summary:
When a TypeVar has
bound=type, pyrefly internally represents the bound astype[Any], which resolves toAttributeBase1::TypeAnyduring attributelookup. The quantified TypeVar attribute resolution only handled
AttributeBase1::ClassInstance, causing any other variant (includingTypeAny)to fall back to
object. This produced false positives like"Object of class
objecthas no attribute__name__" when accessingattributes that exist on the
typeclass.The fix extracts the bound-to-ClassType mapping into a
quantified_bound_classhelper that handles both
ClassInstance(the common case) andTypeAny(the
bound=typecase, mapping tobuiltins.type). This is applied to allfour sites where quantified bounds are resolved: both
Tandtype[T]paths,for both
BoundandConstraintsrestrictions.Fixes #2614
Differential Revision: D95419868