Skip to content

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

@ebk46

Description

@ebk46

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions