-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slice behavior difference in mypy 1.14+dev and 1.13 #18225
Comments
Yeah, I think this is expected. You should use If you just want to check compatibility instead of the "type exact match" that assert_type does, you could do something like: |
@hauntsaninja Well, this is inconsistent. Consider the following example: from typing import reveal_type
class IndexSlice:
def __getitem__(self, arg: slice) -> slice:
return arg
foo = IndexSlice()[2:4]
reveal_type(foo)
foo2 = IndexSlice()[:]
reveal_type(foo2)
So in my original example. I would have expected the same - the type should be |
Your original example used a TypeVar. |
@Dr-Irv The difference between that and the original example is that the original example uses a type variable with an upper bound of def f1(x: list) -> list: return x
def f2[T: list](x: T) -> T: return x
reveal_type(f1([1, 2, 3])) # N: Revealed type is "builtins.list[Any]"
reveal_type(f2([1, 2, 3])) # N: Revealed type is "builtins.list[builtins.int]" |
OK - I see what is happening now. Let me see if I can make the |
closing as a result |
I'm going to reopen this, because now there is a difference in from typing import assert_type
from retslice import IndexSlice, Index
foo = IndexSlice[Index(), :]
reveal_type(foo) pyright output:
mypy 1.14+dev output:
mypy 1.13 output:
From a typing perspective, I think the type of just Based on what I read in the PEP here: https://peps.python.org/pep-0696/#type-parameters-as-parameters-to-generics , I think the |
If I understand what you're saying correctly, I don't think default type arguments are relevant here. (In fact, mypy does not consider them when inferring the type of slice expressions). The reason mypy produces class Foo:
def __getitem__[T](self, item: T) -> T: return item
foo = Foo()
>>> foo[:]
slice(None, None, None) I think pyright's behavior may be from a lack of implementation rather than an intentional behavior (cc @erictraut for comment). It seems that pyright does not currently attempt to infer the generic type parameters for slice expressions: class Foo:
def __getitem__[T](self, item: T) -> T: return item
foo = Foo()
x1 = foo["a"::False]
reveal_type(x1) # mypy: Revealed type is "builtins.slice[builtins.str, None, builtins.bool]"
# pyright: Type of "x1" is "slice[Any, Any, Any]"
x2 = foo[slice("a", None, False)]
reveal_type(x2) # mypy: Revealed type is "builtins.slice[builtins.str, None, builtins.bool]"
# pyright: Type of "x2" is "slice[str, None, bool]" |
Yes, this is missing functionality in pyright. The |
So once this is fixed in
The type checking of code needs to work with both of the above to allow a pull request to be accepted. We also test the in-development version of So for class Foo:
def __getitem__[T](self, item: T) -> T: return item
foo = Foo()
x1 = foo["a"::1]
reveal_type(x1) # mypy: 1.14+dev Revealed type is "builtins.slice[builtins.str, None, builtins.bool]"
# mypy 1.13.0 Revealed type is "builtins.slice"
# pyright: Type of "x1" is "slice[Any, Any, Any]"
x2 = foo[slice("a", None, 1)]
reveal_type(x2) # mypy: Revealed type is "builtins.slice[builtins.str, None, builtins.bool]"
# mypy 1.13.0 Revealed type is "builtins.slice"
# pyright: Type of "x2" is "slice[str, None, bool]" So how in the testing code can I support mypy 1.13.0 which doesn't have the updated typeshed, and 1.14+dev (and soon pyright, once the issue is fixed there) that does have the updated typeshed? Is there a way to say "Use Or, equivalently, can you have code that uses We might just have to pin down the pyright and mypy versions in our CI until mypy 1.14 is released, and then we can upgrade both. |
One option is to use a bool constant with a name like |
Some other options:
|
Thanks @brianschubert . For now, I can use option (1) until both pyright 1.1.390 and mypy 1.14 are released. Will close this. Thanks all for the help. |
We test
pandas-stubs
with the nightly version of mypy. It reveals an issue with how a method that returnsslice
is annotated. Withpandas-stubs
, we get an error like this with the development version ofmypy
:In this case, the
assert_type()
call ofassert_type(pd.IndexSlice[ind, :], tuple["pd.Index[int]", slice])
works with mypy 1.13, fails with the 1.14 dev version.This is due to a change in
typeshed
whereslice
has become generic. I think that change intypeshed
is bundled with the 1.14+dev version of mypy, not the released version 1.13.A simpler example is below. Not sure if this is a
mypy
or atypeshed
issue.Bug Report
mypy 1.14+dev infers a different value for
slice
than 1.13To Reproduce
Requires 2 files.
First, there is
retslice.pyi
:Then there is
mypyslice.py
:Expected Behavior
No errors reported by mypy 1.14+dev
Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneMay be related to #18149
The text was updated successfully, but these errors were encountered: