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

Unable to create alias of WithAnnotation[Model, Extra] - Bad number of arguments for type alias, expected 1, given 2 #2385

Open
ebk46 opened this issue Sep 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ebk46
Copy link

ebk46 commented Sep 27, 2024

Bug report

What's wrong

I am trying to define an alias for an annotated model to be used as parameter type for a function header.

class MyModelExtra(TypedDict):
  foo: str

type MyModelWithExtra = WithAnnotations[MyModel, MyModelExtra]
OR
MyModelWithExtra: TypeAlias = WithAnnotations[MyModel, MyModelExtra]

WithAnnotations is being flagged by Mypy - stacktrace:

/project/types.py:171:43:171:43: error: Bad number of arguments for type alias, expected 1, given 2  [type-arg]
/project/types.py:171:43:171:43: error: Type application is only supported for generic classes  [misc]

I even seem to be getting this error sometimes in places where it's redefined directly as a parameter type rather than an alias. In other words, param: WithAnnotations[MyModel, MyModelExtra]

I've tried defining the type alias in different ways including:

class MyModelWithExtra(WithAnnotations[MyModel, MyModelExtra]):
  pass
  
class MyModelWithExtra(Annotated[MyModel, Annotations[MyModelExtra]]):
  pass

Those methods don't throw the error, but it seems the actual annotation gets dropped and the type winds up just resolving to the standard MyModel.

How is that should be

I expect that WithAnnotations would work as an alias or at least redefined directly. I can't tell why Mypy thinks WithAnnotations takes 1 argument instead of 2.

System information

  • OS: Mac OS 14.7
  • python version: 3.12.6
  • django version: 4.2.15
  • mypy version: 1.11.2
  • django-stubs version: 5.1.0
  • django-stubs-ext version: 5.1.0
@ebk46 ebk46 added the bug Something isn't working label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant