narrow membership tests against named concrete containers (#4469) - #4816
Open
shobhitmehro wants to merge 1 commit into
Open
narrow membership tests against named concrete containers (#4469)#4816shobhitmehro wants to merge 1 commit into
shobhitmehro wants to merge 1 commit into
Conversation
named containers like `set[str]` fell through membership narrowing since only inline literals used their element type. this adds a check for concrete builtin containers (list, set, frozenset, deque) and narrows through the same equality-aware path as `==`, so types that can still compare equal across a nominal boundary, like `float` against `set[int]`, don't get over-narrowed. abstract containers and custom `__contains__` are left alone since their membership check doesn't have to follow equality. fixes facebook#4469
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D118862655. (Because this pull request was imported automatically, there will not be any future comments.) |
|
Diff from mypy_primer, showing the effect of this PR on open source code: ============================================================
SUMMARY
============================================================
Total: +13 new errors, -52 fixed errors
By preset: +3/-44 (default), +3/-44 (strict)
Projects with changes (20):
openlibrary: +2 -3
rotki: +1 -2
pytest: +0 -2
alerta: +3 -3
pwndbg: +1 -0
schemathesis: +0 -1
beartype: +0 -4
streamlit: +0 -3
ibis: +0 -2
zulip: +0 -2
Tanjun: +0 -1
websockets: +0 -2
mypy: +2 -6
meson: +0 -2
core: +1 -6
trio: +0 -2
sphinx: +0 -2
spack: +2 -4
pylint: +1 -4
archinstall: +0 -1
============================================================
FULL DIFF DETAILS
------------------------------------------------------------
openlibrary (https://github.com/internetarchive/openlibrary)
- ERROR openlibrary/catalog/marc/marc_binary.py:137:23-71: Yielded type `tuple[Unknown | None, Unknown]` is not assignable to declared yield type `tuple[str, BinaryDataField | str]` [invalid-yield]
+ ERROR openlibrary/catalog/marc/marc_binary.py:137:23-71: Yielded type `tuple[str | Unknown | None, Unknown]` is not assignable to declared yield type `tuple[str, BinaryDataField | str]` [invalid-yield]
- ERROR openlibrary/catalog/marc/marc_binary.py:139:23-55: Yielded type `tuple[Unknown | None, BinaryDataField]` is not assignable to declared yield type `tuple[str, BinaryDataField | str]` [invalid-yield]
+ ERROR openlibrary/catalog/marc/marc_binary.py:139:23-55: Yielded type `tuple[str | Unknown | None, BinaryDataField]` is not assignable to declared yield type `tuple[str, BinaryDataField | str]` [invalid-yield]
- ERROR openlibrary/core/lending.py:580:59-72: Argument `object | Unknown` is not assignable to parameter `key` with type `str` in function `dict.get` [bad-argument-type]
rotki (https://github.com/rotki/rotki)
- ERROR rotkehlchen/chain/evm/decoding/yearn/decoder.py:166:58-71: Argument `ChecksumAddress | None` is not assignable to parameter `vault_address` with type `ChecksumAddress` in function `_get_vault_token_name` [bad-argument-type]
+ ERROR rotkehlchen/chain/evm/node_inquirer.py:1674:46-47: Unused `# type: ignore` comment [unused-type-ignore]
- ERROR rotkehlchen/serialization/deserialize.py:756:38-46: Argument `ChainID` is not assignable to parameter `chain_id` with type `Literal[ChainID.BASE, ChainID.OPTIMISM, ChainID.SCROLL]` in function `rotkehlchen.externalapis.etherscan_like.EtherscanLikeApi.get_l1_fee` [bad-argument-type]
pytest (https://github.com/pytest-dev/pytest)
- ERROR src/_pytest/config/__init__.py:594:21-597:22: The type of this argument is unknown [unknown-argument-type]
- ERROR src/_pytest/config/__init__.py:596:25-37: Object of class `NoneType` has no attribute `replace` [missing-attribute]
alerta (https://github.com/alerta/alerta)
- ERROR alerta/models/alarms/alerta.py:149:16-22: Implicit conversion of `Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR alerta/models/alarms/alerta.py:149:16-22: Implicit conversion of `str | Unknown | None` to `bool` is not allowed [implicit-bool]
- ERROR alerta/models/alarms/alerta.py:248:16-22: Implicit conversion of `Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR alerta/models/alarms/alerta.py:248:16-22: Implicit conversion of `str | Unknown | None` to `bool` is not allowed [implicit-bool]
- ERROR alerta/models/alarms/alerta.py:248:16-48: Implicit conversion of `Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR alerta/models/alarms/alerta.py:248:16-48: Implicit conversion of `Literal[''] | bool | Unknown | None` to `bool` is not allowed [implicit-bool]
pwndbg (https://github.com/pwndbg/pwndbg)
+ ERROR pwndbg/aglib/elf.py:445:55-56: Unused `# type: ignore` comment [unused-type-ignore]
schemathesis (https://github.com/schemathesis/schemathesis)
- ERROR src/schemathesis/specs/openapi/coverage/_schema.py:1003:17-20: The type of this argument is unknown [unknown-argument-type]
beartype (https://github.com/beartype/beartype)
- ERROR beartype/_check/convert/_reduce/_nonpep/api/redapinumpy.py:305:55-69: Cannot index into `_IsSubclassFactory` [bad-index]
- ERROR beartype/_check/error/_pep/pep484585/errpep484585container.py:65:13-38: Cannot index into `dict[HintSign, range]` [bad-index]
- ERROR beartype/_check/error/_pep/pep484585/errpep484585container.py:124:9-34: Argument `HintSign | None` is not assignable to parameter `key` with type `HintSign` in function `dict.get` [bad-argument-type]
- ERROR beartype/_check/error/_pep/pep484585/errpep484585mapping.py:58:59-84: Cannot index into `dict[HintSign, range]` [bad-index]
streamlit (https://github.com/streamlit/streamlit)
- ERROR lib/streamlit/elements/heading.py:502:20-27: Returned type `Literal[False] | str | None` is not assignable to declared return type `str` [bad-return]
- ERROR lib/streamlit/elements/lib/built_in_chart_utils.py:820:58-66: `not in` is not supported between `None` and `str` [unsupported-operation]
- ERROR lib/streamlit/elements/lib/built_in_chart_utils.py:821:30-38: Argument `str | None` is not assignable to parameter `value` with type `str` in function `list.remove` [bad-argument-type]
ibis (https://github.com/ibis-project/ibis)
- ERROR ibis/backends/tests/test_udf.py:124:25-26: Cannot index into `dict[LiteralString, int]` [bad-index]
- ERROR ibis/backends/tests/test_udf.py:124:25-26: Cannot set item in `dict[LiteralString, int]` [unsupported-operation]
zulip (https://github.com/zulip/zulip)
- ERROR zerver/actions/message_send.py:586:38-66: Argument `list[tuple[int | Unknown, int | Unknown | None]]` is not assignable to parameter `message_triggered_bot_tuples` with type `list[tuple[int, int]]` in function `RecipientInfoResult.__init__` [bad-argument-type]
- ERROR zproject/computed_settings.py:1148:35-56: Argument `str | None` is not assignable to parameter `value` with type `str` in function `list.remove` [bad-argument-type]
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- ERROR tanjun/context/menu.py:144:16-28: Returned type `CommandType` is not assignable to declared return type `Literal[CommandType.MESSAGE, CommandType.USER]` [bad-return]
websockets (https://github.com/aaugustin/websockets)
- ERROR src/websockets/extensions/permessage_deflate.py:275:46-51: Argument `str | None` is not assignable to parameter `x` with type `Buffer | SupportsIndex | SupportsInt | SupportsTrunc | str` in function `int.__new__` [bad-argument-type]
- ERROR src/websockets/extensions/permessage_deflate.py:285:46-51: Argument `str | None` is not assignable to parameter `x` with type `Buffer | SupportsIndex | SupportsInt | SupportsTrunc | str` in function `int.__new__` [bad-argument-type]
mypy (https://github.com/python/mypy)
- ERROR mypy/semanal.py:4435:49-56: `None` is not subscriptable [unsupported-operation]
- ERROR mypy/stubutil.py:672:21-32: `+` is not supported between `Literal['_']` and `None` [unsupported-operation]
- ERROR mypy/stubutil.py:673:12-41: Implicit conversion of `Literal[True] | str | Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR mypy/stubutil.py:673:12-41: Implicit conversion of `Literal[True] | str | None` to `bool` is not allowed [implicit-bool]
- ERROR mypy/stubutil.py:675:16-21: Implicit conversion of `str | Unknown | None` to `bool` is not allowed [implicit-bool]
+ ERROR mypy/stubutil.py:675:16-21: Implicit conversion of `str | None` to `bool` is not allowed [implicit-bool]
- ERROR mypyc/irbuild/util.py:227:13-23: Cannot set item in `MypycAttrs` [unsupported-operation]
- ERROR mypyc/irbuild/util.py:228:19-22: Cannot set item in `dict[MypycAttr, int]` [unsupported-operation]
meson (https://github.com/mesonbuild/meson)
- ERROR mesonbuild/mdevenv.py:236:12-29: `in` is not supported between `Literal['bash']` and `None` [not-iterable]
- ERROR mesonbuild/mdevenv.py:247:14-30: Object of class `NoneType` has no attribute `endswith` [missing-attribute]
core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/deconz/services.py:199:42-57: Argument `str | None` is not assignable to parameter `value` with type `str` in function `list.remove` [bad-argument-type]
- ERROR homeassistant/components/deconz/services.py:206:46-61: Argument `str | None` is not assignable to parameter `value` with type `str` in function `list.remove` [bad-argument-type]
+ ERROR homeassistant/components/mqtt/climate.py:728:41-54: Unnecessary `str()` call; argument is already of type `str` [unnecessary-type-conversion]
- ERROR homeassistant/components/network/__init__.py:85:12-65: Returned type `str | None` is not assignable to declared return type `str` [bad-return]
- ERROR homeassistant/components/shelly/event.py:314:33-43: Argument `Any | None` is not assignable to parameter `event_type` with type `str` in function `homeassistant.components.event.EventEntity._trigger_event` [bad-argument-type]
- ERROR homeassistant/components/tibber/sensor.py:936:33-56: Object of class `NoneType` has no attribute `replace` [missing-attribute]
- ERROR homeassistant/components/yolink/device_trigger.py:90:41-65: Cannot index into `dict[str, set[str]]` [bad-index]
trio (https://github.com/python-trio/trio)
- ERROR src/trio/_highlevel_serve_listeners.py:52:41-50: Cannot index into `Mapping[int, str]` [bad-index]
- ERROR src/trio/_highlevel_serve_listeners.py:53:37-46: Argument `int | None` is not assignable to parameter `code` with type `int` in function `os.strerror` [bad-argument-type]
sphinx (https://github.com/sphinx-doc/sphinx)
- ERROR sphinx/domains/c/_symbol.py:565:50-69: Argument `str | None` is not assignable to parameter `docname` with type `str` in function `Symbol._fill_empty` [bad-argument-type]
- ERROR sphinx/domains/cpp/_symbol.py:941:50-69: Argument `str | None` is not assignable to parameter `docname` with type `str` in function `Symbol._fill_empty` [bad-argument-type]
spack (https://github.com/spack/spack)
- ERROR lib/spack/spack/solver/asp.py:3465:52-62: Argument `str | None` is not assignable to parameter `value` with type `str` in function `list.index` [bad-argument-type]
- ERROR lib/spack/spack/test/cmd/dev_build.py:42:27-63: No matching overload found for function `posixpath.join` called with arguments: (Prefix, object | Unknown) [no-matching-overload]
+ ERROR lib/spack/spack/test/cmd/dev_build.py:60:72-73: Unused `# type: ignore` comment [unused-type-ignore]
+ ERROR lib/spack/spack/test/cmd/dev_build.py:81:72-73: Unused `# type: ignore` comment [unused-type-ignore]
- ERROR lib/spack/spack/test/cmd/dev_build.py:222:27-63: No matching overload found for function `posixpath.join` called with arguments: (Prefix, object | Unknown) [no-matching-overload]
- ERROR lib/spack/spack/test/cmd/dev_build.py:265:27-63: No matching overload found for function `posixpath.join` called with arguments: (Prefix, object | Unknown) [no-matching-overload]
pylint (https://github.com/pycqa/pylint)
- ERROR pylint/checkers/method_args.py:117:16-19: Implicit conversion of `list[str | None]` to `bool` is not allowed [implicit-bool]
+ ERROR pylint/checkers/method_args.py:117:16-19: Implicit conversion of `list[str]` to `bool` is not allowed [implicit-bool]
- ERROR pylint/checkers/nested_min_max.py:105:47-55: Object of class `NodeNG` has no attribute `args` [missing-attribute]
- ERROR pylint/checkers/refactoring/implicit_booleaness_checker.py:224:48-56: Argument `NodeNG | str` is not assignable to parameter `operator` with type `str` in function `ImplicitBooleanessChecker._get_suggestion` [bad-argument-type]
- ERROR pylint/checkers/refactoring/implicit_booleaness_checker.py:244:38-46: Argument `NodeNG | str` is not assignable to parameter `operator` with type `str` in function `ImplicitBooleanessChecker._get_suggestion` [bad-argument-type]
archinstall (https://github.com/archlinux/archinstall)
- ERROR archinstall/lib/menu/list_manager.py:83:43-48: Argument `str | ValueT` is not assignable to parameter `action` with type `str` in function `ListManager.handle_action` [bad-argument-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.
named containers fell through membership narrowing since only inline literals used their element type. this narrows list, set, frozenset, and deque through the same equality-aware path as ==, so it doesn't over-narrow stuff like float against set[int]. abstract containers and custom
__contains__stay untouched since they don't have to follow equality.fixes #4469
Test Plan
python3 test.py