Skip to content
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

[red-knot] Infer subscript expression types #13689

Open
Tracked by #12701
MichaReiser opened this issue Oct 9, 2024 · 4 comments
Open
Tracked by #12701

[red-knot] Infer subscript expression types #13689

MichaReiser opened this issue Oct 9, 2024 · 4 comments
Assignees
Labels
red-knot Multi-file analysis & type inference

Comments

@MichaReiser
Copy link
Member

MichaReiser commented Oct 9, 2024

Limited support for inferring subscript expressions, e.g when the value is a tuple, or string.

def test(a: str):
	c = a[0]
@MichaReiser MichaReiser changed the title subscript expression @MichaReiser [red-knot[ Infer subscript expression types Oct 9, 2024
@MichaReiser MichaReiser changed the title [red-knot[ Infer subscript expression types [red-knot] Infer subscript expression types Oct 9, 2024
@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Oct 9, 2024
@MichaReiser MichaReiser self-assigned this Oct 9, 2024
@MichaReiser
Copy link
Member Author

@carljm String and tuple support has already been implemented in #13562. Do you have something else in mind that goes beyond what's covered in that PR?

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 9, 2024

Following #13562, #13579 and #13667, I think the only thing we don't support with regard to type inference of subscript expressions is where a class does not define __class_getitem__ but its metaclass defines __getitem__. But we don't really support anything regarding metaclasses yet.

@AlexWaygood
Copy link
Member

Oh, it looks like we might not support slices for string-literal and tuple types yet? x[1:3] should be inferred as tuple[str, str] if x has type tuple[int, str, str, int]. y[1:3] should be inferred as Literal["aa"] if y has type Literal["baab"].

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 10, 2024

We could probably also add special handling for subscript expressions where BytesLiteral types are on the l.h.s. and IntLiteral or BooleanLiteral types are on the r.h.s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

No branches or pull requests

2 participants