[ty] Unify Type::is_subtype_of() and Type::is_assignable_to()#18430
[ty] Unify Type::is_subtype_of() and Type::is_assignable_to()#18430AlexWaygood merged 5 commits intomainfrom
Type::is_subtype_of() and Type::is_assignable_to()#18430Conversation
|
7ea7ea5 to
91186b3
Compare
9449576 to
e4088ea
Compare
I'm not totally sure why this diagnostic is being added by this PR. Note that
This error goes away because we now infer the type of the module import readline
if not hasattr(readline, 'set_completion_display_matches_hook'):
reveal_type(readline)This seems like we're doing a better job now according to the information we've been given from typeshed's stubs.
This is a pre-existing bug: astral-sh/ty#578. The reason it's showing up now is that we didn't previously ever consider module-literal types to be subtypes of protocol-instance types, but now we do. |
|
(I don't love the name |
|
great minds think alike 😜 in all seriousness, though -- no, I hadn't seen that before writing this! |
dcreager
left a comment
There was a problem hiding this comment.
I love this overall! My main questions are about whether we should go all the way and add has_assignability_relation_to to all of the subsidiary Type types
| // `target_subclass_ty.subclass_of().into_class()` will be `None` for `type[Any]` or `type[Unknown]`, | ||
| // but that's okay even if we're checking for subtyping, because `type[Any].is_fully_static()` is | ||
| // `false` in our model, so we won't even get here if we're checking for subtyping (it'll be filtered | ||
| // out by the first check in this method). |
There was a problem hiding this comment.
I don't disagree, but is this just so that you don't have to perform a similar refactoring on ClassType?
There was a problem hiding this comment.
Hmm, I don't think so -- we're comparing between two different variants here rather than between two instances of the same variant
There was a problem hiding this comment.
I think I now see what you mean -- does 0385c77 make the changes you were looking for here?
I think we should! I deliberately held off from that here though, as it was already a large change that was tricky to get right, and I didn't want to make it harder to review. I can make that change as part of this PR, but I feel like I'd prefer to do it as a followup? |
I have a slight preference for doing it all in one PR, though I'm happy to defer to you as the person doing the work! (It's a bit easier to turn off my brain and look for ~every call site to become Also note that But again, happy to defer to you!
It's called |
crates/ty_python_semantic/resources/mdtest/intersection_types.md
Outdated
Show resolved
Hide resolved
e4088ea to
39dfb7c
Compare
|
Okay, I think that's all the review comments addressed! There are still one or two |
I'm happy to take this up as a follow-up unless someone else is interested in this :) |
dhruvmanila
left a comment
There was a problem hiding this comment.
I haven't looked too closely as there are already 2 reviewers but a couple of things I noticed while glancing through the changes.
39dfb7c to
4b59858
Compare
4b59858 to
52f2085
Compare
Confirmed that this has now gone from the mypy_primer report following #18466, which fixed astral-sh/ty#578! |
0385c77 to
228eb0a
Compare
228eb0a to
a2ba5b4
Compare
Summary
There's lots of duplication between
Type::is_subtype_of()andType::is_assignable_to(), and we've had multiple bugs in the past because we've remembered to updateType::is_subtype_of()but have not realised thatType::is_assignable_to()also needed to be updated. This PR unifies the two methods: they are now both thin wrappers over aType::has_assignability_relationmethod.Test Plan