python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.56k stars 2.84k forks source link

TypeError: __init__() missing required argument 'readonly_keys' #18177

Closed zachaysan closed 3 days ago

zachaysan commented 3 days ago

Crash Report

Running mypy on the main Flagsmith code repo I'm hitting an error that I can't escape from even if I add the offending file to the exclude list or if I mark the offending line of code that causes the error as # type: ignore.

Traceback

Traceback (most recent call last):
  File "mypy/checkexpr.py", line 5848, in accept
  File "mypy/nodes.py", line 1969, in accept
  File "mypy/checkexpr.py", line 480, in visit_call_expr
  File "mypy/checkexpr.py", line 614, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1471, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1565, in check_call
  File "mypy/checkexpr.py", line 1811, in check_callable_call
  File "mypy/checkexpr.py", line 1262, in apply_function_plugin
  File "/home/zach/.cache/pypoetry/virtualenvs/flagsmith-api-rl5v6uhh-py3.12/lib/python3.12/site-packages/mypy_django_plugin/transformers/querysets.py", line 311, in extract_proper_type_queryset_values
    row_type = helpers.make_typeddict(ctx.api, column_types, set(column_types.keys()))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zach/.cache/pypoetry/virtualenvs/flagsmith-api-rl5v6uhh-py3.12/lib/python3.12/site-packages/mypy_django_plugin/lib/helpers.py", line 355, in make_typeddict
    typed_dict_type = TypedDictType(fields, required_keys=required_keys, fallback=fallback_type)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: __init__() missing required argument 'readonly_keys' (pos 3)

To Reproduce Failure is on the type: ignore line below. The full code for the file is found here

def restrict_use_due_to_api_limit_grace_period_over() -> None:
    """
    Restrict API use once a grace period has ended.

    Since free plans don't have predefined subscription periods, we
    use a rolling thirty day period to filter them.
    """
    now = timezone.now()
    grace_period = now - timedelta(days=API_USAGE_GRACE_PERIOD)
    month_start = now - timedelta(30)
    queryset = (
    OrganisationAPIUsageNotification.objects.filter(  # type: ignore
            Q(
                notified_at__gte=month_start,
        notified_at__lte=grace_period,
                percent_usage__gte=100,
            )
            | Q(
                notified_at__gte=month_start,
                notified_at__lte=now,
                percent_usage__gte=100,
                organisation__breached_grace_period__isnull=False,
            )
        )
        .values("organisation")
        .annotate(max_value=Max("percent_usage"))
    )

Your Environment

brianschubert commented 3 days ago

Hi! This is #17958 / https://github.com/typeddjango/django-stubs/issues/2405. This should be fixed if you upgrade to django-stubs v5.1.1 or newer.

zachaysan commented 16 hours ago

Thank you so much for the help! It works as you suggested :)