fix Ignored issubclass(categories, enum.Enum) check #3283#3339
fix Ignored issubclass(categories, enum.Enum) check #3283#3339asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Diff from mypy_primer, showing the effect of this PR on open source code: bokeh (https://github.com/bokeh/bokeh)
- ERROR src/bokeh/core/property/bases.py:480:30-49: `str` is not assignable to variable `type_param` with type `Property[Any] | type[Property[Any]]` [bad-assignment]
+ ERROR src/bokeh/core/property/instance.py:99:29-48: `type | type[T]` is not assignable to variable `instance_type` with type `type[Serializable]` [bad-assignment]
- ERROR src/bokeh/core/property/instance.py:110:16-29: Returned type `type | type[Serializable]` is not assignable to declared return type `type[T]` [bad-return]
+ ERROR src/bokeh/core/property/instance.py:110:16-29: Returned type `type[Serializable]` is not assignable to declared return type `type[T]` [bad-return]
- ERROR src/bokeh/document/document.py:755:32-40: Argument `dict[str | type[_Operator], Any] | type[Model]` is not assignable to parameter `selector` with type `dict[str | type[_Operator], Any]` in function `Document.select` [bad-argument-type]
- ERROR src/bokeh/model/model.py:553:32-40: Argument `dict[str | type[_Operator], Any] | type[Model]` is not assignable to parameter `selector` with type `dict[str | type[_Operator], Any]` in function `Model.select` [bad-argument-type]
- ERROR src/bokeh/util/callback_manager.py:99:35-40: Cannot index into `dict[str, list[EventCallback]]` [bad-index]
- ERROR src/bokeh/util/callback_manager.py:101:36-41: Argument `str | type[Event]` is not assignable to parameter `element` with type `str` in function `set.add` [bad-argument-type]
pytest (https://github.com/pytest-dev/pytest)
- ERROR src/_pytest/raises.py:425:20-23: Returned type `type[BaseException]` is not assignable to declared return type `type[BaseExcT_1]` [bad-return]
materialize (https://github.com/MaterializeInc/materialize)
+ ERROR misc/python/materialize/cli/mzcompose.py:169:47-61: Type `type[Never]` is not iterable [not-iterable]
beartype (https://github.com/beartype/beartype)
- ERROR beartype/_util/text/utiltextlabel.py:556:41-44: Argument `type` is not assignable to parameter `hint` with type `TypeForm[Any]` in function `beartype._util.hint.pep.proposal.pep484585.generic.pep484585gentest.is_hint_pep484585_generic_subbed` [bad-argument-type]
- ERROR beartype/_util/text/utiltextlabel.py:585:38-41: Argument `type` is not assignable to parameter `hint` with type `TypeForm[Any]` in function `beartype._util.hint.pep.proposal.pep544.is_hint_pep544_protocol` [bad-argument-type]
pytest-autoprofile (https://gitlab.com/TTsangSC/pytest-autoprofile)
- ERROR src/pytest_autoprofile/_test_utils.py:372:24-49: `Collection[type[Warning]] | type[Warning]` is not assignable to variable `warnings` with type `Collection[type[Warning]]` [bad-assignment]
static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/store_config.py:349:31-44: `StoreConfigBase` is not assignable to variable `default` with type `TVStoreConfig | None` [bad-assignment]
pydantic (https://github.com/pydantic/pydantic)
+ ERROR pydantic/v1/fields.py:637:66-76: Expected class object, got `Any` [invalid-argument]
|
There was a problem hiding this comment.
Pull request overview
Fixes incorrect narrowing for patterns combining isinstance(x, type) with issubclass(...), specifically ensuring that known class-object types are preserved and that negative issubclass narrowing correctly distributes over union members (so unions containing both class and non-class values don’t cause narrowing to “give up”).
Changes:
- Preserve class-object types when narrowing
isinstance(x, type)so existingtype[T]/ bare class refs don’t lose precision. - Distribute negative
issubclassnarrowing over union members, allowing class-like members to be removed while keeping non-class members intact. - Add a regression test covering the
isinstance(..., type) and issubclass(...)pattern with anelsebranch expectation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyrefly/lib/alt/narrow.rs |
Adjusts isinstance and negative issubclass narrowing to preserve class-object types and distribute correctly over unions. |
pyrefly/lib/test/narrow.rs |
Adds a regression test reproducing #3283 and asserting correct else-branch narrowing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Primer Diff Classification❌ 3 regression(s) | ✅ 4 improvement(s) | 7 project(s) total | +4, -11 errors 3 regression(s) across pytest, materialize, pydantic. error kinds:
Detailed analysis❌ Regression (3)pytest (-1)
materialize (+1)
pydantic (+1)
✅ Improvement (4)bokeh (+2, -6)
beartype (-2)
pytest-autoprofile (-1)
static-frame (-1)
Suggested fixesSummary: The PR's negative issubclass narrowing changes cause regressions in materialize (type[Never] instead of type[enum.Enum]) and pydantic (Any not handled after isinstance(x, type)), while pytest's -1 appears to be a correctly fixed false positive. 1. In the distribute_over_union closure for negative issubclass narrowing in
2. In the isinstance narrowing special case at lines 401-405 of
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (7 LLM) |
Summary
Fixes #3283
isinstance(x, type) preserves known class-object types so negative issubclass narrowing distributes over union members instead of giving up when the union also contains non-class values.
Test Plan
add test