Skip to content

Comments

[red-knot] Allow CallableTypeFromFunction to display the signatures of callable types that are not function literals#17047

Merged
AlexWaygood merged 2 commits intomainfrom
alex/callabletypefrommethod
Mar 28, 2025
Merged

[red-knot] Allow CallableTypeFromFunction to display the signatures of callable types that are not function literals#17047
AlexWaygood merged 2 commits intomainfrom
alex/callabletypefrommethod

Conversation

@AlexWaygood
Copy link
Member

I found this helpful for understanding some of the stuff that was going on in #17005. It means the name of the special form is perhaps no longer ideal; I could possibly rename it if we agree that this is a useful feature.

… of callable types that are not function literals
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 28, 2025
@AlexWaygood AlexWaygood requested a review from dhruvmanila March 28, 2025 19:31
@github-actions
Copy link
Contributor

github-actions bot commented Mar 28, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

This looks good to me! I would probably rename to CallableTypeOf?

@AlexWaygood AlexWaygood enabled auto-merge (squash) March 28, 2025 20:20
@AlexWaygood
Copy link
Member Author

Renamed it to CallableTypeOf!

@AlexWaygood AlexWaygood merged commit 9305233 into main Mar 28, 2025
22 checks passed
@AlexWaygood AlexWaygood deleted the alex/callabletypefrommethod branch March 28, 2025 20:23
Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Looks good 👍

Comment on lines +456 to +457
# TODO: `self` is bound here; this should probably be `(x: int) -> str`?
reveal_type(c5) # revealed: (self, x: int) -> str
Copy link
Contributor

@MatthewMckee4 MatthewMckee4 Mar 29, 2025

Choose a reason for hiding this comment

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

Should c5 be stored as (x: int) -> str, or stored as (self, x: int) -> str and treated like (x: int) -> str?

@AlexWaygood @carljm

Copy link
Contributor

Choose a reason for hiding this comment

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

Not entirely sure! I think this requires deeper exploration. Today we do the latter. There are a couple interesting questions that it connects to:

  1. How do we handle equivalent callable types? If we aim for "equivalent callable types are always the same Salsa ID" then it should actually be stored as (x: int) -> str. But I don't think it's totally clear yet if that will be feasible.

  2. When do we issue an error on calling a bound method with an annotation on the self argument that doesn't match the instance we accessed it from? Does it happen when accessing the bound method, or when calling it? The latter seems better (matches runtime better), but requires storing the bound self type and the full signature.

It seems likely that we need to support both representations and understand their equivalence. (But maybe our "general callable type" uses only the simpler representation?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants