Respect explicit return type of __new__()#21441
Conversation
This comment has been minimized.
This comment has been minimized.
|
cc @Dr-Irv for all the new errors in class Timestamp(datetime):
...
def __new__( # type: ignore[misc]
cls: type[Self],
# ...
) -> Self | NaTType: ... |
Yes, I agree that is suspicious.
We should probably copy the PYI for I have on my TODO list to first make the pandas source compatible with the latest release of |
Would the following alternatives also preserve backwards compatibility just as well, but reflect explicit intention better?
|
|
@bzoracler Yeah, I actually was thinking about both these options myself. Reasons why I didn't do this:
However, I don't have strong feelings on either, so if others will chime in, I am happy reconsider. |
This comment has been minimized.
This comment has been minimized.
|
It looks like this exposes a pre-existing bug where we leaked type variables in |
This comment has been minimized.
This comment has been minimized.
|
OK, |
|
I mostly agree with bzoracler:
Will look at the diff in a little bit! |
This comment has been minimized.
This comment has been minimized.
|
OMG! We are actually handling arbitrary class object with return type Line 1847 in e53693b |
This comment has been minimized.
This comment has been minimized.
|
@hauntsaninja I tried your (or really @bzoracler's) suggestions. Looking at the primer I am slightly worried this may cause false negatives. It looks like there is still some code doing this: class C:
def __new__(cls) -> Any: # to silence a mypy error from some old version
return super().__new__(cls)Otherwise however the primer looks as expected. |
In this example, are we saying that Hmm, I can only think of this being an issue if |
|
@bzoracler OK, I did some digging and it looks like mypy (until v0.920) used to infer |
Ping on this one. |
This comment has been minimized.
This comment has been minimized.
JukkaL
left a comment
There was a problem hiding this comment.
Did a quick pass, not a full review.
| # determining the return type | ||
| class A: | ||
| def __new__(cls): | ||
| pass | ||
|
|
||
| class B: | ||
| def __new__(cls) -> Any: | ||
| def __new__(cls, x: int = 0) -> Any: |
There was a problem hiding this comment.
What about partial signature, but implicit Any return type? For example, def __new__(self, x: int = 0): ...
There was a problem hiding this comment.
Added a test (works as expected, we infer C as constructor return type).
| def foo(self) -> str: ... | ||
|
|
||
| class A: | ||
| def __new__(cls) -> Foo: ... # E: Incompatible return type for "__new__" (returns "Foo", but must return a subtype of "A") |
There was a problem hiding this comment.
Test if A is compatible with Callable[[], Foo] or Callable[[], A]? Also similarly against callback protocol.
Is it worth testing type joins involving __new__ return type overrides?
There was a problem hiding this comment.
Added a bunch of tests for subtyping (btw this uncovered one type combination I missed). Also added few basic things for joins. All works as expected.
|
|
||
| class C(B): ... | ||
|
|
||
| # Always respect explicit return type after giving an error. |
There was a problem hiding this comment.
This feels a bit inconsistent and causes false negatives. Would changing cause many issues in existing code?
There was a problem hiding this comment.
What do you mean by inconsistent? How is this different from say:
class B:
x: str
class C(B):
x: int # Error
class D(C): ...
reveal_type(C().x) # int
reveal_type(D().x) # intAlso this is kind of the whole point of what people want: respect the explicit return type.
There was a problem hiding this comment.
Ok, I'm convinced that the new behavior is good.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
JukkaL
left a comment
There was a problem hiding this comment.
Looks good -- nice to have this working finally. Left some minor comments, feel free to merge once you've addressed those that make sense.
|
|
||
| class C(B): ... | ||
|
|
||
| # Always respect explicit return type after giving an error. |
There was a problem hiding this comment.
Ok, I'm convinced that the new behavior is good.
| def __call__(self) -> Foo: ... | ||
|
|
||
| def f(x: Callable[[], A]) -> None: ... | ||
| def g(x: Callable[[], Foo]) -> None: ... |
There was a problem hiding this comment.
Test Callable[[], T] -- T should be inferred from the __new__ return type when relevant.
There was a problem hiding this comment.
Yep, there was the same issue in constraints.py as in subtypes.py.
|
|
||
| # This tests the type repr in error messages. | ||
| j = join(A, B) | ||
| x: int = j # E: Incompatible types in assignment (expression has type "type[Base2]", variable has type "int") |
There was a problem hiding this comment.
What about testing isinstance narrowing with a type that has an explicit __new__ return type?
| "from_concatenate": self.from_concatenate, | ||
| "imprecise_arg_kinds": self.imprecise_arg_kinds, | ||
| "unpack_kwargs": self.unpack_kwargs, | ||
| "instance_type": ( | ||
| self.instance_type.serialize() if self.instance_type is not None else None | ||
| ), |
There was a problem hiding this comment.
Add an incremental test case?
This comment has been minimized.
This comment has been minimized.
|
I briefly searched the tracker for issues with |
|
Diff from mypy_primer, showing the effect of this PR on open source code: zope.interface (https://github.com/zopefoundation/zope.interface)
- src/zope/interface/common/tests/test_collections.py:88: error: Incompatible types in assignment (expression has type "set[type[array[int]]]", base class "VerifyClassMixin" defined the type as "tuple[()]") [assignment]
+ src/zope/interface/common/tests/test_collections.py:88: error: Incompatible types in assignment (expression has type "set[type[array[_T]]]", base class "VerifyClassMixin" defined the type as "tuple[()]") [assignment]
comtypes (https://github.com/enthought/comtypes)
- comtypes/server/register.py:226: error: Incompatible types in assignment (expression has type "reversed[tuple[int, str]]", variable has type "list[tuple[int, str]]") [assignment]
+ comtypes/server/register.py:226: error: Incompatible types in assignment (expression has type "Iterator[tuple[int, str]]", variable has type "list[tuple[int, str]]") [assignment]
antidote (https://github.com/Finistere/antidote)
+ src/antidote/lib/interface_ext/predicate.py:118: error: Returning Any from function declared to return "NeutralWeight" [no-any-return]
+ src/antidote/lib/interface_ext/qualifier.py:134: error: Returning Any from function declared to return "NeutralWeight" [no-any-return]
+ tests/lib/interface/test_custom.py:205: error: Returning Any from function declared to return "NeutralWeight" [no-any-return]
discord.py (https://github.com/Rapptz/discord.py)
- discord/abc.py:1815: error: Incompatible types in assignment (expression has type "reversed[MessagePin]", variable has type "list[MessagePin]") [assignment]
+ discord/abc.py:1815: error: Incompatible types in assignment (expression has type "Iterator[MessagePin]", variable has type "list[MessagePin]") [assignment]
sympy (https://github.com/sympy/sympy)
+ sympy/core/evalf.py:997: error: Argument 1 to "evalf_add" has incompatible type "Expr"; expected "Add" [arg-type]
+ sympy/core/function.py:449: error: Unused "type: ignore" comment [unused-ignore]
+ sympy/printing/smtlib.py:188: error: Argument 1 to "_print_Unequality" of "SMTLibPrinter" has incompatible type "Unequality | BooleanFalse | BooleanTrue"; expected "Unequality" [arg-type]
tornado (https://github.com/tornadoweb/tornado)
+ tornado/test/ioloop_test.py:433: error: Unused "type: ignore" comment [unused-ignore]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/tseries/frequencies.py:336: error: Incompatible return value type (got "Timestamp | NaTType", expected "Timestamp") [return-value]
- pandas/core/dtypes/cast.py:698: error: Non-overlapping identity check (left operand type: "Timestamp", right operand type: "NaTType") [comparison-overlap]
- pandas/core/dtypes/cast.py:711: error: Non-overlapping identity check (left operand type: "Timedelta", right operand type: "NaTType") [comparison-overlap]
+ pandas/core/dtypes/cast.py:159: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "str | bytes | date | datetime | timedelta | <7 more items> | Interval[Any] | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]") [assignment]
+ pandas/core/dtypes/cast.py:161: error: Incompatible types in assignment (expression has type "Timedelta | NaTType", variable has type "str | bytes | date | datetime | timedelta | <7 more items> | Interval[Any] | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]") [assignment]
+ pandas/core/arrays/_ranges.py:147: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp | None") [assignment]
+ pandas/core/arrays/datetimes.py:832: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "unit" [union-attr]
+ pandas/core/arrays/datetimes.py:3177: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp | None") [assignment]
+ pandas/core/arrays/datetimes.py:3177: note: Error code "assignment" not covered by "type: ignore[arg-type]" comment
- pandas/core/arrays/datetimes.py:3178: error: Non-overlapping identity check (left operand type: "Timestamp", right operand type: "NaTType") [comparison-overlap]
+ pandas/core/arrays/datetimes.py:3178: error: Non-overlapping identity check (left operand type: "Timestamp | None", right operand type: "NaTType") [comparison-overlap]
+ pandas/core/arrays/datetimes.py:3179: error: Item "None" of "Timestamp | None" has no attribute "as_unit" [union-attr]
+ pandas/core/arrays/datetimes.py:3185: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp | None") [assignment]
+ pandas/core/arrays/datetimes.py:3185: note: Error code "assignment" not covered by "type: ignore[arg-type]" comment
- pandas/core/arrays/datetimes.py:3186: error: Non-overlapping identity check (left operand type: "Timestamp", right operand type: "NaTType") [comparison-overlap]
+ pandas/core/arrays/datetimes.py:3186: error: Non-overlapping identity check (left operand type: "Timestamp | None", right operand type: "NaTType") [comparison-overlap]
+ pandas/core/arrays/datetimes.py:3187: error: Item "None" of "Timestamp | None" has no attribute "as_unit" [union-attr]
+ pandas/core/arrays/period.py:385: error: Incompatible return value type (got "Period | NaTType", expected "Period") [return-value]
+ pandas/core/arrays/period.py:1228: error: Argument 1 to "_add_timedelta_arraylike" of "PeriodArray" has incompatible type "ndarray[tuple[Any, ...], dtype[generic[date | int | timedelta | None]]]"; expected "TimedeltaArray | ndarray[tuple[Any, ...], dtype[timedelta64[timedelta | int | None]]]" [arg-type]
+ pandas/core/arrays/period.py:1343: error: Argument 1 to "delta_to_tick" has incompatible type "Timedelta | NaTType"; expected "timedelta" [arg-type]
+ pandas/core/window/rolling.py:2023: error: Incompatible types in assignment (expression has type "int | signedinteger[_64Bit]", variable has type "int | None") [assignment]
+ pandas/core/tools/datetimes.py:633: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_julian_date" [union-attr]
+ pandas/core/indexes/timedeltas.py:324: error: Argument 1 to "to_offset" has incompatible type "Timedelta | NaTType"; expected "timedelta | str | PeriodDtypeBase" [arg-type]
+ pandas/core/indexes/timedeltas.py:324: note: Error code "arg-type" not covered by "type: ignore[operator]" comment
+ pandas/core/indexes/period.py:673: error: Incompatible return value type (got "Period | NaTType", expected "Period") [return-value]
+ pandas/core/indexes/period.py:700: error: Item "NaTType" of "Period | NaTType" has no attribute "asfreq" [union-attr]
+ pandas/core/indexes/datetimes.py:943: error: Unsupported left operand type for % ("NaTType") [operator]
+ pandas/core/indexes/datetimes.py:943: note: Left operand is of type "Timedelta | NaTType"
+ pandas/core/indexes/datetimes.py:1120: error: Item "NaTType" of "Period | NaTType" has no attribute "start_time" [union-attr]
+ pandas/core/indexes/datetimes.py:1123: error: Unsupported operand types for + ("NaTType" and "int") [operator]
+ pandas/core/indexes/datetimes.py:1123: note: Left operand is of type "Period | NaTType"
+ pandas/core/indexes/datetimes.py:1148: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "datetime") [assignment]
+ pandas/core/indexes/datetimes.py:1154: error: "datetime" has no attribute "tz_localize" [attr-defined]
+ pandas/core/indexes/datetimes.py:1156: error: Incompatible return value type (got "tuple[datetime, Resolution]", expected "tuple[Timestamp, Resolution]") [return-value]
+ pandas/core/indexes/datetimes.py:1735: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "unit" [union-attr]
+ pandas/core/indexes/datetimes.py:1736: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "unit" [union-attr]
+ pandas/core/indexes/datetimelike.py:1139: error: Argument 1 to "to_offset" has incompatible type "Timedelta | NaTType"; expected "timedelta | str | PeriodDtypeBase" [arg-type]
+ pandas/core/resample.py:2524: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Literal['epoch', 'start', 'start_day', 'end', 'end_day'] | Timestamp") [assignment]
+ pandas/core/resample.py:2648: error: Argument "offset" to "_get_timestamp_range_edges" has incompatible type "Timedelta | NaTType | None"; expected "Timedelta | None" [arg-type]
+ pandas/core/resample.py:2840: error: Argument "offset" to "_get_period_range_edges" has incompatible type "Timedelta | NaTType | None"; expected "Timedelta | None" [arg-type]
+ pandas/core/resample.py:2959: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Literal['epoch', 'start', 'start_day', 'end', 'end_day'] | Timestamp") [assignment]
+ pandas/core/resample.py:2975: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp") [assignment]
+ pandas/core/resample.py:2977: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp") [assignment]
+ pandas/core/resample.py:2979: error: Incompatible types in assignment (expression has type "Timestamp | NaTType", variable has type "Timestamp") [assignment]
+ pandas/core/resample.py:3091: error: No overload variant of "__mul__" of "signedinteger" matches argument type "Tick" [operator]
+ pandas/core/resample.py:3091: note: Possible overload variants:
+ pandas/core/resample.py:3091: note: def __mul__(self, int | signedinteger[_8Bit] | numpy.bool[builtins.bool] | signedinteger[_64Bit], /) -> signedinteger[_64Bit]
+ pandas/core/resample.py:3091: note: def __mul__(self, float, /) -> float64
+ pandas/core/resample.py:3091: note: def __mul__(self, complex, /) -> complex128
+ pandas/core/resample.py:3091: note: def __mul__(self, signedinteger[Any], /) -> signedinteger[Any]
+ pandas/core/resample.py:3091: note: def __mul__(self, integer[Any], /) -> Any
+ pandas/core/resample.py:3091: note: Left operand is of type "int | signedinteger[_64Bit]"
+ pandas/core/resample.py:3139: error: Incompatible return value type (got "tuple[Timestamp | NaTType, Timestamp | NaTType]", expected "tuple[Timestamp, Timestamp]") [return-value]
+ pandas/io/excel/_odfreader.py:220: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "time" [union-attr]
+ pandas/tseries/holiday.py:262: error: Incompatible types in assignment (expression has type "Timestamp | NaTType | None", variable has type "Timestamp | None") [assignment]
+ pandas/tseries/holiday.py:264: error: Incompatible types in assignment (expression has type "Timestamp | NaTType | None", variable has type "Timestamp | None") [assignment]
+ pandas/tests/tslibs/test_timedeltas.py:24: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:277: error: Unsupported operand types for - ("NaTType" and "Nano") [operator]
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:277: note: Left operand is of type "Timestamp | NaTType"
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:278: error: Unsupported operand types for - ("NaTType" and "Nano") [operator]
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:278: note: Left operand is of type "Timestamp | NaTType"
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:288: error: Unsupported operand types for - ("NaTType" and "Nano") [operator]
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:288: note: Left operand is of type "Timestamp | NaTType"
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:289: error: Unsupported operand types for - ("NaTType" and "Nano") [operator]
+ pandas/tests/tseries/offsets/test_custom_business_hour.py:289: note: Left operand is of type "Timestamp | NaTType"
+ pandas/tests/tseries/offsets/test_business_day.py:78: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/tseries/offsets/test_business_day.py:79: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/series/indexing/test_setitem.py:512: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/series/indexing/test_setitem.py:513: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/scalar/test_nat.py:557: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/scalar/test_nat.py:558: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/scalar/timedelta/test_timedelta.py:266: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "max" [union-attr]
+ pandas/tests/scalar/interval/test_arithmetic.py:116: error: Value of type variable "_OrderableT" of "Interval" cannot be "Timestamp | NaTType" [type-var]
+ pandas/tests/scalar/interval/test_arithmetic.py:119: error: Value of type variable "_OrderableT" of "Interval" cannot be "Timedelta | NaTType" [type-var]
+ pandas/tests/indexes/test_engines.py:62: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/indexes/test_engines.py:63: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:72: error: List item 0 has incompatible type "list[Timestamp | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:79: error: List item 1 has incompatible type "list[Timestamp | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:86: error: List item 2 has incompatible type "list[Timedelta | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:93: error: List item 3 has incompatible type "list[Period | Any]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:94: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:95: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:96: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:97: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:98: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:202: error: List item 0 has incompatible type "list[float | Timestamp | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:212: error: List item 1 has incompatible type "list[float | Timestamp | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:222: error: List item 2 has incompatible type "list[float | Timedelta | NaTType]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:232: error: List item 3 has incompatible type "list[float | Period | Any]"; expected "ndarray[tuple[Any, ...], dtype[Any]]" [list-item]
+ pandas/tests/groupby/methods/test_rank.py:233: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:234: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:236: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:237: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/groupby/methods/test_rank.py:238: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/arrays/test_timedeltas.py:215: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/arrays/test_timedeltas.py:216: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arrays/test_timedeltas.py:238: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/arrays/test_datetimes.py:669: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "to_period" [union-attr]
+ pandas/tests/arrays/test_datetimelike.py:187: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "time" [union-attr]
+ pandas/tests/arithmetic/conftest.py:70: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arithmetic/conftest.py:72: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/arithmetic/conftest.py:87: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arithmetic/conftest.py:106: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arithmetic/conftest.py:131: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/plotting/_matplotlib/converter.py:153: error: Item "NaTType" of "Timestamp | NaTType" has no attribute "time" [union-attr]
+ pandas/plotting/_matplotlib/converter.py:309: error: Item "NaTType" of "Period | NaTType" has no attribute "ordinal" [union-attr]
+ pandas/conftest.py:1575: error: Incompatible return value type (got "Timestamp | NaTType", expected "Timestamp") [return-value]
+ pandas/tests/arithmetic/test_timedelta64.py:87: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/arithmetic/test_period.py:42: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arithmetic/test_period.py:59: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
+ pandas/tests/arithmetic/test_numeric.py:227: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_timedelta64" [union-attr]
+ pandas/tests/arithmetic/test_numeric.py:228: error: Item "NaTType" of "Timedelta | NaTType" has no attribute "to_pytimedelta" [union-attr]
... (truncated 8 lines) ...
jax (https://github.com/google/jax)
- jax/experimental/pallas/ops/tpu/splash_attention/splash_attention_mask_info.py:729: error: Incompatible types in assignment (expression has type "ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]]", variable has type "list[ndarray[tuple[Any, ...], dtype[Any]]] | None") [assignment]
+ jax/experimental/pallas/ops/tpu/splash_attention/splash_attention_mask_info.py:729: error: Incompatible types in assignment (expression has type "ndarray[tuple[Any, ...], dtype[bool[Any]]]", variable has type "list[ndarray[tuple[Any, ...], dtype[Any]]] | None") [assignment]
starlette (https://github.com/encode/starlette)
+ starlette/testclient.py:143: error: Incompatible types in assignment (expression has type "tuple[MemoryObjectSendStream[MutableMapping[str, Any]], MemoryObjectReceiveStream[MutableMapping[str, Any]]]", variable has type "create_memory_object_stream[MutableMapping[str, Any]]") [assignment]
+ starlette/testclient.py:145: error: Incompatible types in assignment (expression has type "tuple[MemoryObjectSendStream[MutableMapping[str, Any]], MemoryObjectReceiveStream[MutableMapping[str, Any]]]", variable has type "create_memory_object_stream[MutableMapping[str, Any]]") [assignment]
+ starlette/testclient.py:692: error: Incompatible types in assignment (expression has type "tuple[MemoryObjectSendStream[MutableMapping[str, Any] | None], MemoryObjectReceiveStream[MutableMapping[str, Any] | None]]", variable has type "create_memory_object_stream[MutableMapping[str, Any] | None]") [assignment]
+ starlette/testclient.py:694: error: Incompatible types in assignment (expression has type "tuple[MemoryObjectSendStream[MutableMapping[str, Any]], MemoryObjectReceiveStream[MutableMapping[str, Any]]]", variable has type "create_memory_object_stream[MutableMapping[str, Any]]") [assignment]
+ starlette/middleware/base.py:189: error: Incompatible types in assignment (expression has type "tuple[MemoryObjectSendStream[MutableMapping[str, Any]], MemoryObjectReceiveStream[MutableMapping[str, Any]]]", variable has type "create_memory_object_stream[MutableMapping[str, Any]]") [assignment]
|
|
This is awesome; thanks! |
Fixes #8330
Fixes #15182
Closes #16020
Closes #14471
Fixes #13824
Fixes #14502
With this PR will still give an error if the explicit return
__new__()is not a subtype of current class, but now we will actually use it. There are two exceptions (to preserve backwards compatibility):Anywith still use current class as the return type.This uses a more principled implementation than some earlier attempts: adding a new dedicated attribute to
CallableTypefor this purpose. Some comments:CallableType.type_guardandCallableType.type_iswere not handled in dependency visitors (neither coarse-grained nor fine-grained). IMO they definitely should be handled there, so I now handle them.CallableTypea bit to compensate for new attribute by removing (rarely used)min_argsattribute. I also use compact flags serialization.CallableType.ret_typefor various type object edge cases.instance_typeis set whenis_type_obj()returnsTrue. This is mostly to avoid breaking 3rd party plugins.type[T]. It is relatively niche edge case, but it looks important for NumPy stubs, so I am trying to fix it here.