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

Add plugin ability to defer to default type analyzer #17544

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kcdodd
Copy link

@kcdodd kcdodd commented Jul 20, 2024

  • The essence of this PR is to allow a Plugin's callback for get_type_analyze_hook to defer back to the default implementation.
  • The motivating use-case that lead to needing this was specifically to help customizing type analysis for a variadic generic class which could be used in contexts (like inheritance) where the arguments would be an unpacked TypeVarTuple. The mechanisms for dealing with this seem complicated, and not exposed via the plugin API (but I could be mistaken on that). As far as I could tell it requires specialized logic for enabling/handling the unpacking etc, and besides these are not the instances that need to be customized in this case.
  • The approach here is to return NotImplemented as a sentinel to indicate the intention. This appears to not require altering the annotated return Type (but I'm not familiar with why that is the case), but it seems that for example returning None would require the callback signature to change, which might be an irritation. Also, in my personal experience this is preferable to raising say NotImplementError since the intent can be less clear as to where exactly the error was raised compared to a return value.
  • The included change appears to be the only place where the hook is called, and seems to not alter the succeeding logic if it was simply ignored.
  • I am somewhat new to the mypy source style, so I apologize if this PR doesn't conform to conventions.

Following is an example of the use-case mentioned above...

# example use case
def _analyze_type(ctx: AnalyzeTypeContext) -> Type:
  for arg in ctx.type.args:
    if isinstance(arg, UnboundType) and arg.name == 'Unpack':
      return NotImplemented
    ...

@kcdodd kcdodd changed the title Add ability to defer to default type analyzer Add plugin ability to defer to default type analyzer Jul 20, 2024

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@kcdodd kcdodd marked this pull request as ready for review July 20, 2024 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant