Add RegularCallableTypeOf and into_regular_callable in ty_extensions#23909
Add RegularCallableTypeOf and into_regular_callable in ty_extensions#23909dhruvmanila merged 8 commits intomainfrom
RegularCallableTypeOf and into_regular_callable in ty_extensions#23909Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.29%. The percentage of expected errors that received a diagnostic held steady at 78.13%. The number of fully passing files held steady at 64/132. |
|
Memory usage reportMemory usage unchanged ✅ |
|
Oh, of course this would mean that the test cases that is relying on this behavior would fail 🤦 |
CallableTypeOf to always use Regular callable type kindRegularCallableTypeOf and into_regular_type in ty_extensions
|
There are some things to do:
But, I'm opening this up for feedback (and name bike shedding ;)) before I make those large changes. |
| def f(a: int, b: str, /) -> None: ... | ||
|
|
||
| static_assert(is_assignable_to(Callable[[int, str], None], RegularCallableTypeOf[f])) | ||
| static_assert(is_subtype_of(Callable[[int, str], None], RegularCallableTypeOf[f])) | ||
| static_assert(is_assignable_to(Callable[[int, str], None], TypeOf[into_regular_callable(f)])) | ||
| static_assert(is_subtype_of(Callable[[int, str], None], TypeOf[into_regular_callable(f)])) |
There was a problem hiding this comment.
I guess another solution would be to use a callback protocol for these kinds of assertions? This protocol:
class Foo(Protocol):
def __call__(self, *, x: int): ...is equivalent to this type:
def func(*, x: int): ...
type Foo = RegularCallableTypeOf[func]and then we don't need another special form. But the disadvantage of that is that we're relying on our Protocol subtyping/assignability checks being correct, and there are some complications there. So it's probably better to go with what you have right now.
There was a problem hiding this comment.
Yes, definitely! I think on a long-term horizon Protocol is the way to go here and then we can remove these variants.
Relatedly, I've been using Protocol to do these relation checks locally sometimes and I haven't faced any issues yet :)
crates/ty_python_semantic/src/types/infer/builder/type_expression.rs
Outdated
Show resolved
Hide resolved
RegularCallableTypeOf and into_regular_type in ty_extensionsRegularCallableTypeOf and into_regular_callable in ty_extensions
|
Superficially this reminds me a bit of astral-sh/ty#1452 / #23560 |
4c85833 to
7592954
Compare
|
(Going to look at the replacement again tomorrow morning to make sure they're all correct and then merge.) |
|
So, the convention is to always use the regular variants unless you explicitly wants to test against the function-like behavior of the callable. Another convention would've been to always use the regular variants when you're checking a relation between two callables except in cases where you want to check the relation which involves at least one function-like callable, and use non-regular variant in other tests. I'm going with the former in this PR. |
* main: (94 commits) Fix shell injection via `shell=True` in subprocess calls (#23894) [ty] Refactor `relation.rs` to store state on a struct rather than passing around 7 arguments every time we recurse (#23837) Don't return code actions for non-Python documents (#23905) [ty] Make the default database truly statically infallible (#23929) [ty] Add `Download` button to ty playground which creates a zip export (#23478) [ty] Respect `kw_only` overwrites in dataclasses (#23930) [ty] Clarify in diagnostics that `from __future__ import annotations` only stringifies type annotations (#23928) [ty] Add a `Copy Markdown` button to playground (#23002) [ty] Fix folding range classification of lines starting with `#` (#23831) [ty] Fix folding ranges for notebooks (#23830) [ty] fix too-many-cycle panics when inferring literal type loop variables (#23875) Add `RegularCallableTypeOf` and `into_regular_callable` in `ty_extensions` (#23909) [ty] treat properties as full structural types (#23925) [ty] Avoid duplicated work during multi-inference (#23923) [ty]: make `possibly-missing-attribute` ignored by default [ty]: split out `possibly-missing-submodule` from `possibly-missing-attribute` Update astral-sh/setup-uv action to v7.5.0 (#23922) [ty] Show truthiness in ConstraintSet display and simplify falsy error message (#23913) Bump 0.15.6 (#23919) [ty] Narrow type context during collection literal inference (#23844) ...
Summary
fixes: astral-sh/ty#3020
Test Plan
Add a regression test case that currently fails on
main.