Repository: python/mypy
Pull request: Ask user about __init__ on duplicate packages
Fixes #5743

Problem statement:
Add a note suggesting to add __init__.py in case of duplicate modules
For example:
```
dir/lib.py: error: Duplicate module named 'lib'
dir/lib.py: note: Are you missing '__init__.py'?
```
Probably we should use some heuristic to add this note only when it makes sense (e.g. there is at least one directory level between the file and the place where mypy is running).

Hints from issue discussion:
working on this

Commit message: Ask user about __init__ on duplicate packages

Fixes #5743

Patch to review:
diff --git a/mypy/build.py b/mypy/build.py
index 17b2cb5453df..17b585439823 100644
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -12,15 +12,16 @@
 
 import binascii
 import contextlib
+import errno
 import gc
 import hashlib
 import json
 import os
+import pathlib
 import re
 import stat
 import sys
 import time
-import errno
 import types
 
 from typing import (AbstractSet, Any, Dict, Iterable, Iterator, List,
@@ -2601,6 +2602,15 @@ def load_graph(sources: List[BuildSource], manager: BuildManager,
                 -1, -1,
                 "Duplicate module named '%s' (also at '%s')" % (st.id, graph[st.id].xpath)
             )
+            p1 = len(pathlib.PurePath(st.xpath).parents)  # type: int
+            p2 = len(pathlib.PurePath(graph[st.id].xpath).parents)  # type: int
+
+            if p1 != p2:
+                manager.errors.report(
+                    -1, -1,
+                    "Are you missing an __init__.py?"
+                )
+
             manager.errors.raise_error()
         graph[st.id] = st
         new.append(st)


Code review comments:
diff_hunk: @@ -2601,6 +2602,15 @@ def load_graph(sources: List[BuildSource], manager: BuildManager,
                 -1, -1,
                 "Duplicate module named '%s' (also at '%s')" % (st.id, graph[st.id].xpath)
             )
+            p1 = len(pathlib.PurePath(st.xpath).parents)  # type: int
+            p2 = len(pathlib.PurePath(graph[st.id].xpath).parents)  # type: int
path: mypy/build.py
text: @user1:
Do you really need these type comments? Mypy should be able to infer that `len()` gives an `int`.