Skip to content

Fix subclassing member with type and list literal context false positive - #22366

Closed
Sudhakar7J wants to merge 5 commits into
python:masterfrom
Sudhakar7J:fix/subclass-member-type-and-list-item
Closed

Sudhakar7J wants to merge 5 commits into
python:masterfrom
Sudhakar7J:fix/subclass-member-type-and-list-item

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Summary

Fixes two mypy false positives.

  1. Subclassing a member access (obj.attr) with type type. mypy rejected
    class Sub(person.characteristics): pass even though characteristics: 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 type though pyright does (e.g. pyright 1.1.414) #22337.

  2. List literal context with list[bool] | Hashable | MutableSequence[str].
    mypy reported ["a", "b"] as an invalid list[bool]. The union simplification
    dropped MutableSequence[str]'s option because MutableSequence[str] is a
    subtype of Hashable. Constraint inference now keeps options for items removed
    by simplification when they could still match. Fixes False positive list-item: list literal checked against list[bool] when the union also contains Hashable #22342.

Changes

  • mypy/semanal.py: defer validation of member-access base classes whose
    attribute type is not known yet; record them on TypeInfo.deferred_member_bases.
  • mypy/checker.py: validate deferred member bases in visit_class_def.
  • mypy/nodes.py: add deferred_member_bases field to TypeInfo.
  • mypy/constraints.py: recover constraint options lost during union
    simplification via _options_lost_by_simplification.

Testing

  • mypy/test/testcheck.py: 830 passed (new testSubclassMemberWithType,
    testSubclassMemberWithNonType, testSubclassMemberWithTypeDisallowAny,
    testListLiteralContextWithUnhashableListAndHashableSequence).
  • New fixture test-data/unit/fixtures/list_hashable.pyi.

Related

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Sudhakar7J and others added 4 commits November 5, 2026 00:12
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.
@Sudhakar7J
Sudhakar7J force-pushed the fix/subclass-member-type-and-list-item branch from ae65ff1 to 0ca2ae3 Compare November 4, 2026 18:43
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
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

@ilevkivskyi ilevkivskyi closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants