diff --git a/importlab/resolve.py b/importlab/resolve.py
index 23314bc..d55f34d 100644
--- a/importlab/resolve.py
+++ b/importlab/resolve.py
@@ -102,6 +102,9 @@ def infer_module_name(filename, fspath):
     for f in fspath:
         short_name = f.relative_path(filename)
         if short_name:
+            # The module name for __init__.py files is the directory.
+            if short_name.endswith(os.path.sep + "__init__"):
+                short_name = short_name[:short_name.rfind(os.path.sep)]
             return short_name.replace(os.path.sep, '.')
     # We have not found filename relative to anywhere in pythonpath.
     return ''

Incorrect inferred module name for __init__.py files
See google/pytype#154 for more detail.

`resolve.infer_module_name` calculates the wrong name for `__init__.py` files. For example, for `foo/bar/__init__.py`, it will return `foo.bar.__init__`. The module name should be `foo.bar`.
diff --git a/tests/test_resolve.py b/tests/test_resolve.py
index 2a217d3..9891764 100644
--- a/tests/test_resolve.py
+++ b/tests/test_resolve.py
@@ -293,6 +293,15 @@ class TestResolverUtils(unittest.TestCase):
                     resolve.infer_module_name("/some/random/file", fspath),
                     "")
 
+    def testInferInitModuleName(self):
+        with utils.Tempdir() as d:
+            os_fs = fs.OSFileSystem(d.path)
+            fspath = [os_fs]
+            py_file = d.create_file("foo/__init__.py")
+            self.assertEqual(
+                    resolve.infer_module_name(py_file, fspath),
+                    "foo")
+
     def testGetAbsoluteName(self):
         test_cases = [
                 ("x.y", "a.b", "x.y.a.b"),

pip install -e .
parse_log_pytest
pytest
pytest
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
certifi==2021.5.30
decorator==4.4.2
-e git+https://github.com/google/importlab.git@77f04151272440dacea197b8c4f74aa26fdbe950#egg=importlab
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1631916693255/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
networkx==2.5.1
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
six==1.17.0
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work

name: importlab
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib-metadata=4.8.1=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - decorator==4.4.2
      - networkx==2.5.1
      - six==1.17.0
prefix: /opt/conda/envs/importlab

tests/test_resolve.py::TestResolverUtils::testInferInitModuleName
tests/test_resolve.py::TestResolver::testFallBackToSource tests/test_resolve.py::TestResolver::testGetPyFromPycSource tests/test_resolve.py::TestResolver::testOverrideSource tests/test_resolve.py::TestResolver::testPycSourceWithoutPy tests/test_resolve.py::TestResolver::testResolveBuiltin tests/test_resolve.py::TestResolver::testResolveInitFile tests/test_resolve.py::TestResolver::testResolveInitFileRelative tests/test_resolve.py::TestResolver::testResolveModuleFromFile tests/test_resolve.py::TestResolver::testResolvePackageFile tests/test_resolve.py::TestResolver::testResolveParentPackageFile tests/test_resolve.py::TestResolver::testResolveParentPackageFileWithModule tests/test_resolve.py::TestResolver::testResolvePyiFile tests/test_resolve.py::TestResolver::testResolveRelativeFromInitFileWithModule tests/test_resolve.py::TestResolver::testResolveRelativeInNonPackage tests/test_resolve.py::TestResolver::testResolveSamePackageFile tests/test_resolve.py::TestResolver::testResolveSiblingPackageFile tests/test_resolve.py::TestResolver::testResolveStarImport tests/test_resolve.py::TestResolver::testResolveStarImportBuiltin tests/test_resolve.py::TestResolver::testResolveStarImportSystem tests/test_resolve.py::TestResolver::testResolveSymbolFromFile tests/test_resolve.py::TestResolver::testResolveSystemInitFile tests/test_resolve.py::TestResolver::testResolveSystemPackageDir tests/test_resolve.py::TestResolver::testResolveSystemRelative tests/test_resolve.py::TestResolver::testResolveSystemSymbol tests/test_resolve.py::TestResolver::testResolveSystemSymbolNameClash tests/test_resolve.py::TestResolver::testResolveTopLevel tests/test_resolve.py::TestResolver::testResolveWithFilesystem tests/test_resolve.py::TestResolverUtils::testGetAbsoluteName tests/test_resolve.py::TestResolverUtils::testInferModuleName
importlab/resolve.py
importlab/resolve.py