Skip to content

WithAnnotations[X, Y] gives runtime error (but typechecker is happy) #719

@jordanbray

Description

@jordanbray

Bug report

What's wrong

I am trying to use the WithAnnotations as documented with two parameters. The first is a django model, and the second is a typed dictionary. Here is a minified example:

from typing import TypedDict
from django_stubs_ext import WithAnnotations
from django.contrib.auth.models import User
  
class MinedData(TypedDict):
    """Use data science to get the information we need."""

    nose_hair_length: float

# Option 1: As Documented
def potential_customer(user: WithAnnotations[User, MinedData]) -> bool:
    return user.nose_hair_length > 2.0


# Option 2: As I tried to write it
PotentialRazorCustomer = WithAnnotations[User, MinedData]

def potential_customer2(user: PotentialRazorCustomer) -> bool:
    return user.nose_hair_length > 2.0
$ mypy --strict tester.py

Works as anticipated. However, when I try to run the program (in this case, with python manage.py shell < tester.py) the traceback for both Option 1 and Option 2 above is the same (save line numbers):

Traceback (most recent call last):
  File "/.../manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/.../lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/.../lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.../lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.../lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/.../lib/python3.9/site-packages/django/core/management/commands/shell.py", line 93, in handle
    exec(sys.stdin.read(), globals())
  File "<string>", line 12, in <module>
  File "/usr/lib/python3.9/typing.py", line 275, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.9/typing.py", line 758, in __getitem__
    _check_generic(self, params, len(self.__parameters__))
  File "/usr/lib/python3.9/typing.py", line 212, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too many parameters for typing.Annotated[~_T, django_stubs_ext.annotations.Annotations[+_Annotations]]; actual 2, expected 1

How is that should be

The above should typecheck correctly, and also run.

System information

  • OS: Linux (I use Arch, btw)
  • python version: 3.9.6
  • django version: 3.2
  • mypy version: 0.910
  • django-stubs version: 1.9.0
  • django-stubs-ext version: 0.3.1

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