Skip to content

Inline configuration for a newer Python version crashes for union syntax on types (PEP 604) #18450

@bzoracler

Description

@bzoracler

Crash Report & To Reproduce

Running the following under mypy installed on a Python 3.9 runtime causes a crash:

# mypy: python-version=3.10

a = int | str

To clarify, setting the equivalent of python-version=3.10 doesn't cause a crash if it was done in the mypy configuration file or command line flags.

As an aside, the docs do note that python-version should only be set in the [global] section of configuration files. Maybe attempts to set global-only sections inline in Python sources should also show an inline error when running mypy? Apart from this python-version option in this case, setting such options inline usually "work", but they seem to be applied across the entirety of all files that are type-checked, and side-effects caused by these options can be difficult to trace the origin of.

Traceback

See mypy playground:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 119, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 203, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 191, in build
    result = _build(
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2935, in dispatch
    process_graph(graph, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3333, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3434, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2307, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 467, in check_first_pass
    self.accept(d)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 576, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1345, in accept
    return visitor.visit_assignment_stmt(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 2998, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 3176, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 4347, in check_simple_assignment
    rvalue_type = self.expr_checker.accept(
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 5903, in accept
    typ = node.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 2108, in accept
    return visitor.visit_op_expr(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 3411, in visit_op_expr
    return self.accept(e.analyzed)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 5903, in accept
    typ = node.accept(self)
          ^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 2710, in accept
    return visitor.visit_type_alias_expr(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4785, in visit_type_alias_expr
    return self.alias_type_in_runtime_context(alias.node, ctx=alias, alias_definition=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4849, in alias_type_in_runtime_context
    return self.chk.named_generic_type("types.UnionType", item.items)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 7102, in named_generic_type
    info = self.lookup_typeinfo(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 7109, in lookup_typeinfo
    sym = self.lookup_qualified(fullname)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 7211, in lookup_qualified
    raise KeyError(msg.format(last, name))
KeyError: "Failed qualified lookup: 'UnionType' (fullname = 'types.UnionType')."

main.py:3: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.15.0+dev.9274a07bfa3c92b38fe35cf9736beb068ae9196b
main.py:3: : note: use --pdb to drop into pdb

Your Environment

  • Mypy version used: 1.14.1, master
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions