Repository navigation
Fix subclassing member with type and list literal context false positive - #22366
Closed
Sudhakar7J wants to merge 5 commits into
Closed
Sudhakar7J wants to merge 5 commits into
Sudhakar7J wants to merge 5 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Fix two mypy false positives: 1. Allow subclassing a base class that is a member access (obj.attr) when the attribute has type type (or Any). Previously mypy rejected it even though pyright accepts it. Deferred member bases are validated by the type checker after the attribute type is known. Fixes python#22337. 2. When inferring constraints for a union actual type, do not drop options for items removed by union simplification. In particular, keep MutableSequence[str]'s option when list[bool] | Hashable | MutableSequence[str] is simplified to list[bool] | Hashable, so f(["a", "b"]) is not reported as an invalid list[bool]. Fixes python#22342.
for more information, see https://pre-commit.ci
…regression for inferred class attrs
Sudhakar7J
force-pushed
the
fix/subclass-member-type-and-list-item
branch
from
November 4, 2026 18:43
ae65ff1 to
0ca2ae3
Compare
for more information, see https://pre-commit.ci
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: operator (https://github.com/canonical/operator)
- ops/_private/harness.py:431: error: Name "self._charm_cls" is not defined [name-defined]
scrapy (https://github.com/scrapy/scrapy)
+ tests/test_utils_datatypes.py:229: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_utils_datatypes.py:229: error: Variable "tests.test_utils_datatypes.TestCaseInsensitiveDict.dict_class" is not valid as a type [valid-type]
+ tests/test_utils_datatypes.py:229: note: Error code "valid-type" not covered by "type: ignore[misc, name-defined]" comment
+ tests/test_utils_datatypes.py:229: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:416: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:416: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:416: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:416: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:429: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:429: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:429: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:429: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:447: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:447: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:447: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:447: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:474: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:474: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:474: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:474: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:529: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:529: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:529: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:529: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:628: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:628: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:628: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:628: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:717: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:717: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:717: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:717: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider_sitemap.py:816: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider_sitemap.py:816: error: Variable "tests.test_spider_sitemap.TestSitemapSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider_sitemap.py:816: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider_sitemap.py:816: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:75: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:75: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:75: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:75: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:111: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:111: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:111: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:111: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:129: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:129: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:129: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:129: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:146: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:146: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:146: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:146: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:158: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:158: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:158: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:158: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:177: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:177: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:177: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:177: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:194: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:194: error: Variable "tests.test_spider.TestXMLFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:194: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:194: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:218: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:218: error: Variable "tests.test_spider.TestCSVFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:218: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:218: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:233: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:233: error: Variable "tests.test_spider.TestCSVFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:233: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:233: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:255: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:255: error: Variable "tests.test_spider.TestCSVFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:255: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:255: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ tests/test_spider.py:267: error: Unused "type: ignore[name-defined]" comment [unused-ignore]
+ tests/test_spider.py:267: error: Variable "tests.test_spider.TestCSVFeedSpider.spider_class" is not valid as a type [valid-type]
+ tests/test_spider.py:267: note: Error code "valid-type" not covered by "type: ignore[name-defined, misc]" comment
+ tests/test_spider.py:267: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexes/base.py:2143: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/core/indexes/base.py:2143: error: List item 0 has incompatible type "Hashable | Sequence[Hashable] | dict[Hashable, Hashable]"; expected "Hashable" [list-item]
+ pandas/core/indexes/base.py:2143: note: Error code "list-item" not covered by "type: ignore[assignment]" comment
|
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.
Summary
Fixes two mypy false positives.
Subclassing a member access (
obj.attr) with typetype. mypy rejectedclass Sub(person.characteristics): passeven thoughcharacteristics: type,while pyright accepts it. Deferred member bases are now validated by the type
checker once the attribute type is known. Fixes MyPy does not support subclassing function a argument member with type
typethough pyright does (e.g. pyright 1.1.414) #22337.List literal context with
list[bool] | Hashable | MutableSequence[str].mypy reported
["a", "b"]as an invalidlist[bool]. The union simplificationdropped
MutableSequence[str]'s option becauseMutableSequence[str]is asubtype of
Hashable. Constraint inference now keeps options for items removedby simplification when they could still match. Fixes False positive
list-item:listliteral checked againstlist[bool]when the union also containsHashable#22342.Changes
mypy/semanal.py: defer validation of member-access base classes whoseattribute type is not known yet; record them on
TypeInfo.deferred_member_bases.mypy/checker.py: validate deferred member bases invisit_class_def.mypy/nodes.py: adddeferred_member_basesfield toTypeInfo.mypy/constraints.py: recover constraint options lost during unionsimplification via
_options_lost_by_simplification.Testing
mypy/test/testcheck.py: 830 passed (newtestSubclassMemberWithType,testSubclassMemberWithNonType,testSubclassMemberWithTypeDisallowAny,testListLiteralContextWithUnhashableListAndHashableSequence).test-data/unit/fixtures/list_hashable.pyi.Related
typethough pyright does (e.g. pyright 1.1.414) #22337list-item:listliteral checked againstlist[bool]when the union also containsHashable#22342