Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting 'item' attribute as an instance created earlier leads to an internal error #15380

Closed
xiaxinmeng opened this issue Jun 6, 2023 · 0 comments · Fixed by #15404
Closed
Labels

Comments

@xiaxinmeng
Copy link

The code sets up a class with a 'NamedTuple' type and initializes an instance of that class with a specified field. The "item" attribute of the instance holds an instance of NamedTuple created earlier. Mypy reports an internal error.

Example.py

from typing import NamedTuple
class Test:
    def __init__(self, field):
        self.Item = NamedTuple("x", [(field, str)])
        self.item: self.Item

The expected behavior:

It does not report an internal error.

The actual behavior:

/home/xxm/Desktop/example.py:7: 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.4.0+dev.66bd2c441277b29b1454e26b2a3454a0dae5fb18
Traceback (most recent call last):
  File "/home/xxm/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 2926, in dispatch
    process_graph(graph, manager)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 3324, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 3419, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 95, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 253, in process_functions
    process_top_level_function(
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 292, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 350, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 603, in refresh_partial
    self.accept(node)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 6352, in accept
    node.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 786, in accept
    return visitor.visit_func_def(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 833, in visit_func_def
    self.analyze_func_def(defn)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 897, in analyze_func_def
    self.analyze_function_body(defn)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 1390, in analyze_function_body
    defn.body.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1220, in accept
    return visitor.visit_block(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 4562, in visit_block
    self.accept(s)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 6352, in accept
    node.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1307, in accept
    return visitor.visit_assignment_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 2824, in visit_assignment_stmt
    self.record_special_form_lvalue(s)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/semanal.py", line 3045, in record_special_form_lvalue
    assert isinstance(lvalue, NameExpr)
AssertionError: 
/home/xxm/Desktop/example.py:7: : note: use --pdb to drop into pdb

Test environment:

Mypy version: 1.4.0+ (master)
Python version: 3.11.3
Operating System: Ubuntu 18.04
How to install MyPy: "python3 -m pip install -U git+https://github.com/python/mypy.git"

ilevkivskyi added a commit that referenced this issue Jun 9, 2023
Fixes #15380

Note I also update the `NamedTuple` fixture to be much closer to real
definition in `typing.pyi` in typeshed.
jhance pushed a commit that referenced this issue Jun 12, 2023
Fixes #15380

Note I also update the `NamedTuple` fixture to be much closer to real
definition in `typing.pyi` in typeshed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants