negative refinement for type(X) != Y, X.__class__ != Y when Y is final#2566
negative refinement for type(X) != Y, X.__class__ != Y when Y is final#2566yangdanny97 wants to merge 2 commits intofacebook:mainfrom
Conversation
Summary: Fixes facebook#1163 Added a special-case facet narrow so `x.__class__ is/== T` narrows `x like isinstance(x, T)`. Pull Request resolved: facebook#2475 Test Plan: Added coverage for `__class__` attribute narrowing with `assert/if` and `==`. Differential Revision: D94521571 Pulled By: yangdanny97
|
@yangdanny97 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D94528111. |
rchen152
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
This comment has been minimized.
This comment has been minimized.
facebook#2566) Summary: Pull Request resolved: facebook#2566 Previously we did not narrow in the negative case for `type(X)` and `X.__class__`, because it only checks whether something is _exactly_ that type. However, if the RHS is a final class then we know there can be no subclasses, so it would be safe to narrow it away. fixes facebook#2530 Reviewed By: rchen152 Differential Revision: D94528111
7f33895 to
720c686
Compare
|
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/pydantic/pydantic)
- ERROR pydantic/networks.py:244:16-76: Returned type `Literal[False] | SupportsBool` is not assignable to declared return type `bool` [bad-return]
- ERROR pydantic/networks.py:247:16-76: Returned type `Literal[False] | SupportsBool` is not assignable to declared return type `bool` [bad-return]
- ERROR pydantic/networks.py:250:16-77: Returned type `Literal[False] | SupportsBool` is not assignable to declared return type `bool` [bad-return]
- ERROR pydantic/networks.py:253:16-77: Returned type `Literal[False] | SupportsBool` is not assignable to declared return type `bool` [bad-return]
werkzeug (https://github.com/pallets/werkzeug)
- ERROR tests/test_wrappers.py:372:16-28: Object of class `Response` has no attribute `foo` [missing-attribute]
static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/frame.py:1307:31-32: Yielded type `Iterable[Any]` is not assignable to declared yield type `ndarray[Any, Any]` [invalid-yield]
- ERROR static_frame/core/frame.py:1444:31-32: Yielded type `Iterable[Any]` is not assignable to declared yield type `ndarray[Any, Any]` [invalid-yield]
- ERROR static_frame/core/frame.py:9295:24-41: Returned type `Self@Frame` is not assignable to declared return type `Frame[Any, Any, *tuple[Any, ...]]` [bad-return]
- ERROR static_frame/core/frame.py:9296:20-24: Returned type `Self@Frame` is not assignable to declared return type `Frame[Any, Any, *tuple[Any, ...]]` [bad-return]
- ERROR static_frame/core/index_hierarchy.py:1204:56-63: Argument `IndexHierarchy[*tuple[Any, ...]] | PendingRow | Sequence[TLabel]` is not assignable to parameter `iterable` with type `Iterable[Sequence[TLabel]]` in function `enumerate.__new__` [bad-argument-type]
- ERROR static_frame/core/type_blocks.py:1764:20-50: `>` is not supported between `int` and `object` [unsupported-operation]
- ERROR static_frame/core/type_blocks.py:3346:34-75: `Generator[Iterable[Any] | ndarray[Any, Any], None, None]` is not assignable to variable `other_operands` with type `Iterable[ndarray[Any, Any]]` [bad-assignment]
- ERROR static_frame/core/util.py:2099:26-2104:10: No matching overload found for function `numpy._core.multiarray.arange` called with arguments: (start=Unknown, stop=Unknown, step=Unknown, dtype=dtype[Any] | Unknown | None) [no-matching-overload]
+ ERROR static_frame/core/util.py:2099:26-2104:10: No matching overload found for function `numpy._core.multiarray.arange` called with arguments: (start=int, stop=int, step=int, dtype=dtype[Any] | Unknown | None) [no-matching-overload]
- ERROR static_frame/test/unit/test_metadata.py:126:26-36: Object of class `IndexBase` has no attribute `dtype` [missing-attribute]
jax (https://github.com/google/jax)
- ERROR jax/_src/interpreters/ad.py:157:14-24: Argument `Sequence[bool] | bool | tuple[bool, ...]` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
- ERROR jax/_src/interpreters/ad.py:160:14-25: Argument `Sequence[bool] | bool | tuple[bool, ...]` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
- ERROR jax/_src/interpreters/ad.py:161:57-68: Argument `Sequence[bool] | bool | tuple[bool, ...]` is not assignable to parameter `iterable` with type `Iterable[bool]` in function `tuple.__new__` [bad-argument-type]
- ERROR jax/_src/interpreters/ad.py:162:33-43: Argument `Sequence[bool] | bool | tuple[bool, ...]` is not assignable to parameter `iterable` with type `Iterable[bool]` in function `tuple.__new__` [bad-argument-type]
- ERROR jax/_src/interpreters/ad.py:1318:51-62: Argument `Sequence[bool] | bool | tuple[bool, ...]` is not assignable to parameter `iterable` with type `Iterable[bool]` in function `tuple.__new__` [bad-argument-type]
|
facebook#2566) Summary: Pull Request resolved: facebook#2566 Previously we did not narrow in the negative case for `type(X)` and `X.__class__`, because it only checks whether something is _exactly_ that type. However, if the RHS is a final class then we know there can be no subclasses, so it would be safe to narrow it away. fixes facebook#2530 Differential Revision: D94528111 Reviewed By: rchen152
Primer Diff Classification✅ 4 improvement(s) | 4 project(s) total 4 improvement(s) across pydantic, werkzeug, static-frame, jax.
Detailed analysis✅ Improvement (4)pydantic (-4)
werkzeug (-1)
static-frame (+1, -9)
jax (-5)
Classification by primer-classifier (4 LLM) · Was this helpful? React with 👍 or 👎 |
facebook#2566) Summary: Pull Request resolved: facebook#2566 Previously we did not narrow in the negative case for `type(X)` and `X.__class__`, because it only checks whether something is _exactly_ that type. However, if the RHS is a final class then we know there can be no subclasses, so it would be safe to narrow it away. fixes facebook#2530 Differential Revision: D94528111 Reviewed By: rchen152
facebook#2566) Summary: Pull Request resolved: facebook#2566 Previously we did not narrow in the negative case for `type(X)` and `X.__class__`, because it only checks whether something is _exactly_ that type. However, if the RHS is a final class then we know there can be no subclasses, so it would be safe to narrow it away. fixes facebook#2530 Differential Revision: D94528111 Reviewed By: rchen152
|
This pull request has been merged in d984d74. |
Summary:
Previously we did not narrow in the negative case for
type(X)andX.__class__, because it only checks whether something is exactly that type. However, if the RHS is a final class then we know there can be no subclasses, so it would be safe to narrow it away.fixes #2530
Differential Revision: D94528111