Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Infer generic type arguments for slice expressions#18160

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@brianschubert
Copy link
Collaborator

Fixes#18149

Slices were made generic inpython/typeshed#11637. Currently, all slice expressions are inferred to have typeslice[Any, Any, Any]. This PR fills in the generic type arguments more appropriately using start/stop/stride expression types.

Given

classFoo:def__getitem__[T](self,item:T)->T:returnitemx=Foo()reveal_type(x[1:])

Before:

main.py:5: note: Revealed type is "builtins.slice[Any, Any, Any]"

After:

main.py:5: note: Revealed type is "builtins.slice[builtins.int, None, None]"

@brianschubert
Copy link
CollaboratorAuthor

As an alternative approach, I toyed with using theslice.__new__ overloads from typeshed to infer the type arguments. However, this had a big impact on the tests (since all tests involving slice expressions now needed a fixture definingslice), and I'm not sure if there are any benefits from doing it that way.

Details
--- a/mypy/checkexpr.py+++ b/mypy/checkexpr.py@@ -5616,7 +5616,12 @@ class ExpressionChecker(ExpressionVisitor[Type]):             if index:                 t = self.accept(index)                 self.chk.check_subtype(t, expected, index, message_registry.INVALID_SLICE_INDEX)-        return self.named_type("builtins.slice")+        args = [+            TempNode(NoneType()) if arg is None else arg+            for arg in [e.begin_index, e.end_index, e.stride]+        ]+        slice_type = TypeType(self.named_type("builtins.slice"))+        return self.check_call(slice_type, args, [nodes.ARG_POS] * 3, e, [None] * 3)[0]

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice! Can we recordt in the loop so we avoid type checking the index expressions twice?

@brianschubert
Copy link
CollaboratorAuthor

brianschubert commentedNov 18, 2024
edited
Loading

Sure, done!

(FWIW, that's how Ioriginally wrote it 😉. I thought to try separating this logic from that loop since I figured it may be removed soon, now that generic slices let us do better checking on slice indices than the hardcoded SupportsIndex/None checks in that loop)

hauntsaninja reacted with heart emoji

@github-actions
Copy link
Contributor

Diff frommypy_primer, showing the effect of this PR on open source code:

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)+ tests/test_frame.py:2246: error: Expression is of type "tuple[Index[int], slice[None, None, None]]", not "tuple[Index[int], slice[Any, Any, Any]]"  [assert-type]- tests/test_frame.py:226: error: Expression is of type "Any", not "DataFrame"  [assert-type]- tests/test_frame.py:227: error: Expression is of type "Any", not "DataFrame"  [assert-type]

@hauntsaninjahauntsaninja merged commit6759dbd intopython:masterNov 19, 2024
19 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@hauntsaninjahauntsaninjahauntsaninja left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Change in inference of overloads involvingHashable andslice now thatslice is generic

2 participants

@brianschubert@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp