python / mypy

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

dmypy: AssertionError: builtins.int crash for running a second time w/ source changes #14645

Open zzzeek opened 1 year ago

zzzeek commented 1 year ago

This issue is very similar to #12744, and I've found two of our checkouts that can reproduce this one with dmypy 1.0.0, steps are below

  1. Python 3.10.0 on this end if it matters, using mypy / dmypy 1.0.0 released on pypi
  2. git clone SQLAlchemy from gerrit and cd into new directory:
git clone https://gerrit.sqlalchemy.org/sqlalchemy/sqlalchemy
cd sqlalchemy
  1. download the first specific patch:
git fetch https://gerrit.sqlalchemy.org/sqlalchemy/sqlalchemy refs/changes/32/4432/2 && git checkout FETCH_HEAD
  1. start and run dmypy on lib

    dmypy start
    dmypy check lib
  2. checkout second specific patch:

git fetch https://gerrit.sqlalchemy.org/sqlalchemy/sqlalchemy refs/changes/32/4432/3 && git checkout FETCH_HEAD
  1. run dmypy again
dmypy check lib

for me, I get stack trace:

Daemon crashed!
Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 229, in serve
  File "mypy/dmypy_server.py", line 274, in run_command
  File "mypy/dmypy_server.py", line 352, in cmd_check
  File "mypy/dmypy_server.py", line 411, in check
  File "mypy/dmypy_server.py", line 596, in fine_grained_increment_follow_imports
  File "mypy/server/update.py", line 267, in update
  File "mypy/server/update.py", line 369, in update_one
  File "mypy/server/update.py", line 452, in update_module
  File "mypy/server/update.py", line 881, in propagate_changes_using_dependencies
  File "mypy/server/update.py", line 1009, in reprocess_nodes
  File "mypy/semanal_main.py", line 137, in semantic_analysis_for_targets
  File "mypy/semanal_main.py", line 220, in process_top_levels
  File "mypy/semanal_main.py", line 348, in semantic_analyze_target
  File "mypy/semanal.py", line 595, in refresh_partial
  File "mypy/semanal.py", line 606, in refresh_top_level
  File "mypy/semanal.py", line 6212, in accept
  File "mypy/errors.py", line 1167, in report_internal_error
  File "mypy/semanal.py", line 6210, in accept
  File "mypy/nodes.py", line 1301, in accept
  File "mypy/semanal.py", line 2702, in visit_assignment_stmt
  File "mypy/semanal.py", line 3441, in check_and_set_up_type_alias
  File "mypy/semanal.py", line 3337, in analyze_alias
  File "mypy/typeanal.py", line 155, in analyze_type_alias
  File "mypy/types.py", line 883, in accept
  File "mypy/typeanal.py", line 253, in visit_unbound_type
  File "mypy/typeanal.py", line 388, in visit_unbound_type_nonoptional
  File "mypy/typeanal.py", line 587, in try_analyze_special_unbound_type
  File "mypy/typeanal.py", line 1327, in analyze_literal_type
  File "mypy/typeanal.py", line 1393, in analyze_literal_param
  File "mypy/typeanal.py", line 1572, in named_type
  File "mypy/typeanal.py", line 250, in lookup_fully_qualified
  File "mypy/semanal.py", line 5525, in lookup_fully_qualified
AssertionError: builtins.int
asqui commented 1 year ago

I also get this failure when I check the entire source directory (of an entirely different, closed source, project), and then immediately re-check a simple file with an error (without any changes):

# src/scratch.py
class A:
    pass

A().foo()
$ time dmypy run -- src/ ; time dmypy run -- src/scratch.py
Daemon started
src/scratch.py:5: error: "A" has no attribute "foo"  [attr-defined]
Found 1 error in 1 file (checked 2952 source files)

real    2m8.413s
user    0m0.264s
sys     0m0.174s
-------------------------------------------------------------------------------
stderr:
/jump/software/rhel8/python311_mypy-1.3.0/lib/python3.11/site-packages/mypy/typeshed/stdlib/builtins.pyi:198: 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.3.0
Daemon crashed!
Traceback (most recent call last):
  File "mypy/dmypy_server.py", line 230, in serve
  File "mypy/dmypy_server.py", line 277, in run_command
  File "mypy/dmypy_server.py", line 345, in cmd_run
  File "mypy/dmypy_server.py", line 414, in check
  File "mypy/dmypy_server.py", line 663, in fine_grained_increment_follow_imports
  File "mypy/server/update.py", line 267, in update
  File "mypy/server/update.py", line 369, in update_one
  File "mypy/server/update.py", line 452, in update_module
  File "mypy/server/update.py", line 881, in propagate_changes_using_dependencies
  File "mypy/server/update.py", line 1009, in reprocess_nodes
  File "mypy/semanal_main.py", line 137, in semantic_analysis_for_targets
  File "mypy/semanal_main.py", line 220, in process_top_levels
  File "mypy/semanal_main.py", line 349, in semantic_analyze_target
  File "mypy/semanal.py", line 599, in refresh_partial
  File "mypy/semanal.py", line 610, in refresh_top_level
  File "mypy/semanal.py", line 6271, in accept
  File "mypy/errors.py", line 1177, in report_internal_error
  File "mypy/semanal.py", line 6269, in accept
  File "mypy/nodes.py", line 1295, in accept
  File "mypy/semanal.py", line 2770, in visit_assignment_stmt
  File "mypy/semanal.py", line 3504, in check_and_set_up_type_alias
  File "mypy/semanal.py", line 3405, in analyze_alias
  File "mypy/typeanal.py", line 154, in analyze_type_alias
  File "mypy/types.py", line 889, in accept
  File "mypy/typeanal.py", line 252, in visit_unbound_type
  File "mypy/typeanal.py", line 387, in visit_unbound_type_nonoptional
  File "mypy/typeanal.py", line 586, in try_analyze_special_unbound_type
  File "mypy/typeanal.py", line 1313, in analyze_literal_type
  File "mypy/typeanal.py", line 1379, in analyze_literal_param
  File "mypy/typeanal.py", line 1558, in named_type
  File "mypy/typeanal.py", line 249, in lookup_fully_qualified
  File "mypy/semanal.py", line 5584, in lookup_fully_qualified
AssertionError: builtins.int

real    7m55.396s
user    0m0.184s
sys     0m0.100s

Additional mystery: Why did it take 8 minutes to re-check a single file and reach this failure, when checking the entire src/ tree (including this file) only took 2 minutes the first time around?

meshy commented 8 months ago

I've come across this in the course of investigating https://github.com/python/mypy/pull/15043.

This is as small as I can get a failing example:

foo.py

import math

math.floor(sum(range(1)))

bar.py (empty)

Commands:

dmypy run -- foo.py
dmypy run -- bar.py

Results:

$ dmypy run -- foo.py
Daemon started
Success: no issues found in 1 source file

$ dmypy run -- bar.py
/.../mypy/typeshed/stdlib/builtins.pyi:220: 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.9.0+dev.f9e8e0bda5cfbb54d6a8f9e482aa25da28a1a635
/.../mypy/typeshed/stdlib/builtins.pyi: 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.9.0+dev.f9e8e0bda5cfbb54d6a8f9e482aa25da28a1a635
Daemon crashed!
Traceback (most recent call last):
  File "/.../mypy/dmypy_server.py", line 236, in serve
    resp = self.run_command(command, data)
  File "/.../mypy/dmypy_server.py", line 285, in run_command
    ret = method(self, **data)
  File "/.../mypy/dmypy_server.py", line 353, in cmd_run
    return self.check(sources, export_types, is_tty, terminal_width)
  File "/.../mypy/dmypy_server.py", line 429, in check
    messages = self.fine_grained_increment_follow_imports(
  File "/.../mypy/dmypy_server.py", line 695, in fine_grained_increment_follow_imports
    messages = fine_grained_manager.update([], to_delete)
  File "/.../mypy/server/update.py", line 267, in update
    result = self.update_one(
  File "/.../mypy/server/update.py", line 369, in update_one
    result = self.update_module(next_id, next_path, next_id in removed_set, followed)
  File "/.../mypy/server/update.py", line 452, in update_module
    remaining += propagate_changes_using_dependencies(
  File "/.../mypy/server/update.py", line 881, in propagate_changes_using_dependencies
    triggered |= reprocess_nodes(manager, graph, id, nodes, deps, processed_targets)
  File "/.../mypy/server/update.py", line 1010, in reprocess_nodes
    semantic_analysis_for_targets(graph[module_id], nodes, graph, saved_attrs)
  File "/.../mypy/semanal_main.py", line 137, in semantic_analysis_for_targets
    process_top_levels(graph, [state.id], patches)
  File "/.../mypy/semanal_main.py", line 220, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/.../mypy/semanal_main.py", line 349, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/.../mypy/semanal.py", line 597, in refresh_partial
    self.refresh_top_level(node)
  File "/.../mypy/semanal.py", line 608, in refresh_top_level
    self.accept(d)
  File "/.../mypy/semanal.py", line 6544, in accept
    report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
  File "/.../mypy/errors.py", line 1261, in report_internal_error
    raise err
  File "/.../mypy/semanal.py", line 6542, in accept
    node.accept(self)
  File "/.../mypy/nodes.py", line 1310, in accept
    return visitor.visit_assignment_stmt(self)
  File "/.../mypy/semanal.py", line 2860, in visit_assignment_stmt
    if self.check_and_set_up_type_alias(s):
  File "/.../mypy/semanal.py", line 3617, in check_and_set_up_type_alias
    res, alias_tvars, depends_on, qualified_tvars, empty_tuple_index = self.analyze_alias(
  File "/.../mypy/semanal.py", line 3506, in analyze_alias
    analyzed, depends_on = analyze_type_alias(
  File "/.../mypy/typeanal.py", line 156, in analyze_type_alias
    res = type.accept(analyzer)
  File "/.../mypy/types.py", line 955, in accept
    return visitor.visit_unbound_type(self)
  File "/.../mypy/typeanal.py", line 259, in visit_unbound_type
    typ = self.visit_unbound_type_nonoptional(t, defining_literal)
  File "/.../mypy/typeanal.py", line 399, in visit_unbound_type_nonoptional
    special = self.try_analyze_special_unbound_type(t, fullname)
  File "/.../mypy/typeanal.py", line 624, in try_analyze_special_unbound_type
    return self.analyze_literal_type(t)
  File "/.../mypy/typeanal.py", line 1470, in analyze_literal_type
    analyzed_types = self.analyze_literal_param(i + 1, arg, t)
  File "/.../mypy/typeanal.py", line 1536, in analyze_literal_param
    fallback = self.named_type(arg.base_type_name)
  File "/.../mypy/typeanal.py", line 1735, in named_type
    node = self.lookup_fully_qualified(fully_qualified_name)
  File "/.../mypy/typeanal.py", line 256, in lookup_fully_qualified
    return self.api.lookup_fully_qualified(name)
  File "/.../mypy/semanal.py", line 5855, in lookup_fully_qualified
    assert ret is not None, fullname
AssertionError: builtins.int