Repository: python/mypy

Issue:
Crash when following imports in daemon
To reproduce, first create a file like this (`t.py`):
```
import pymssql
```

Then follow these steps:
1. Ensure stubs are missing (`pip uninstall types-pymssql`)
2. Run daemon with missing stubs (`dmypy run -- -2 t.py`)
3. Install stubs (`pip install types-pymssql`)
4. `dmypy run -- -2 t.py` -> ok
5. `dmypy run -- -2 t.py` -> crash

Here's the traceback:
```
Daemon crashed!
Traceback (most recent call last):
  File "/Users/jukka/src/mypy/mypy/dmypy_server.py", line 221, in serve
    resp = self.run_command(command, data)
  File "/Users/jukka/src/mypy/mypy/dmypy_server.py", line 264, in run_command
    return method(self, **data)
  File "/Users/jukka/src/mypy/mypy/dmypy_server.py", line 323, in cmd_run
    return self.check(sources, is_tty, terminal_width)
  File "/Users/jukka/src/mypy/mypy/dmypy_server.py", line 385, in check
    messages = self.fine_grained_increment_follow_imports(sources)
  File "/Users/jukka/src/mypy/mypy/dmypy_server.py", line 551, in fine_grained_increment_follow_imports
    messages = fine_grained_manager.update(changed, [])
  File "/Users/jukka/src/mypy/mypy/server/update.py", line 243, in update
    result = self.update_one(changed_modules, initial_set, removed_set)
  File "/Users/jukka/src/mypy/mypy/server/update.py", line 309, in update_one
    result = self.update_module(next_id, next_path, next_id in removed_set)
  File "/Users/jukka/src/mypy/mypy/server/update.py", line 374, in update_module
    result = update_module_isolated(module, path, manager, previous_modules, graph,
  File "/Users/jukka/src/mypy/mypy/server/update.py", line 603, in update_module_isolated
    state.type_check_first_pass()
  File "/Users/jukka/src/mypy/mypy/build.py", line 2135, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/jukka/src/mypy/mypy/checker.py", line 294, in check_first_pass
    self.accept(d)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 403, in accept
    report_internal_error(err, self.errors.file, stmt.line, self.errors, self.options)
  File "/Users/jukka/src/mypy/mypy/errors.py", line 744, in report_internal_error
    raise err
  File "/Users/jukka/src/mypy/mypy/checker.py", line 401, in accept
    stmt.accept(self)
  File "/Users/jukka/src/mypy/mypy/nodes.py", line 1073, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 2010, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/jukka/src/mypy/mypy/checker.py", line 2121, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue,
  File "/Users/jukka/src/mypy/mypy/checker.py", line 2975, in check_simple_assignment
    rvalue_type = self.expr_checker.accept(rvalue, lvalue_type,
  File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3905, in accept
    report_internal_error(err, self.chk.errors.file,
  File "/Users/jukka/src/mypy/mypy/errors.py", line 744, in report_internal_error
    raise err
  File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3903, in accept
    typ = node.accept(self)
  File "/Users/jukka/src/mypy/mypy/nodes.py", line 1490, in accept
    return visitor.visit_name_expr(self)
  File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 179, in visit_name_expr
    result = self.analyze_ref_expr(e)
  File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 206, in analyze_ref_expr
    result = type_object_type(node, self.named_type)
  File "/Users/jukka/src/mypy/mypy/checkmember.py", line 929, in type_object_type
    init_index = info.mro.index(init_method.node.info)
ValueError: <TypeInfo datetime.time> is not in list
```

This also repros with various other stub packages.


Discussion:

Resolving patch:
diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py
--- a/mypy/dmypy_server.py
+++ b/mypy/dmypy_server.py
@@ -620,12 +620,12 @@ def refresh_file(module: str, path: str) -> List[str]:
 
         fix_module_deps(graph)
 
-        # Store current file state as side effect
-        self.fswatcher.find_changed()
-
         self.previous_sources = find_all_sources_in_build(graph)
         self.update_sources(self.previous_sources)
 
+        # Store current file state as side effect
+        self.fswatcher.find_changed()
+
         t5 = time.time()
 
         manager.log("fine-grained increment: update: {:.3f}s".format(t5 - t1))


Test patch: