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] add Type::SubclassOf support to Type::call and Type::call_unbound #15948

Open
carljm opened this issue Feb 4, 2025 · 2 comments
Open
Assignees
Labels
help wanted Contributions especially welcome red-knot Multi-file analysis & type inference

Comments

@carljm
Copy link
Contributor

carljm commented Feb 4, 2025

Description

Existing type checkers (and the typing spec) allow calling an object of type type[C] (in red-knot, Type::SubclassOf(<Class C>)), with the result of the call being an object of type C (in red-knot, Type::Instance(<Class C>)).

This is not actually sound, because Liskov compatibility is not enforced on type constructor methods (__init__ and __new__) when subclassing, so you can't be sure that a constructor call that works for a class C will actually work for a subclass of C. But it's widely relied on, and we will have to support it.

I would like to have an opt-in diagnostic emitted whenever you call an object of type[C], so users who want to avoid this unsoundness have that option.

@carljm carljm added red-knot Multi-file analysis & type inference help wanted Contributions especially welcome labels Feb 4, 2025
@carljm
Copy link
Contributor Author

carljm commented Feb 4, 2025

On second thought, it may be best to hold off on this issue (or at least the new-diagnostic part of it) until after @AlexWaygood completes an in-progress refactor of CallOutcome, since this diagnostic would likely imply adding a new outcome variant to CallOutcome?

@mishamsk
Copy link
Contributor

mishamsk commented Feb 4, 2025

I can take it whenever @AlexWaygood is ready since I've triggered the issue in the first place. Not sure how you manage blocked issues, but feel free to assign this one to me and I'll monitor the comments

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

No branches or pull requests

2 participants