google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.73k stars 276 forks source link

Fix type errors newly discovered with Python 3.12 #1754

Closed copybara-service[bot] closed 3 weeks ago

copybara-service[bot] commented 3 weeks ago

Fix type errors newly discovered with Python 3.12

Python 3.12 changes the bytecode for dict/list/set comprehensions. They no longer compile into functions. They're now inlined.

Pytype checks untyped functions only N levels deep. N=2 with --quick, else N=3. With comprehensions being inlined, pytype will essentially check 1 level deeper now.

This fixes type errors in pytype's own codebase discovered by this change. For easier review, these were the error messages:

annotation_utils.py:136:21: error: in _do_sub_one_annotation: Function AnnotationUtils.sub_annotations_for_parameterized_class.<locals>.get_type_parameter_subst was called with the wrong arguments [wrong-arg-types]
         Expected: (annotation: pytype.abstract._typing.TypeParameter)
  Actually passed: (annotation: pytype.abstract._base.BaseValue)

        done.append(get_type_parameter_subst_fn(cur))
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

errors/error_printer.py:13:51: error: in _print_typed_dict_error: No attribute 'missing' on pytype.errors.error_types.NonIterableStrError [attribute-error]
  In Optional[Union[pytype.errors.error_types.NonIterableStrError, pytype.errors.error_types.TypedDictError]]

    ret += "TypedDict missing keys: " + ", ".join(error.missing)
                                                  ~~~~~~~~~~~~~