From 229bc9042c184e4e4f1fd3111b799aad8154fbbf Mon Sep 17 00:00:00 2001 From: Karim Khaleel Date: Sat, 26 Apr 2025 22:03:18 -0400 Subject: [PATCH] fix annotating __init__.py files by stripping __init__ from modpath --- auto_type_annotate.py | 1 + tests/auto_type_annotate_test.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/auto_type_annotate.py b/auto_type_annotate.py index c0f1f77..9a7b123 100644 --- a/auto_type_annotate.py +++ b/auto_type_annotate.py @@ -36,6 +36,7 @@ def _to_mod(fname: str, roots: tuple[str, ...]) -> str: relative.removesuffix('.py') .replace('/', '.') .replace('\\', '.') + .removesuffix('.__init__') ) else: raise AssertionError(f'{fname=} not found in {roots=}') diff --git a/tests/auto_type_annotate_test.py b/tests/auto_type_annotate_test.py index 4437427..40f3677 100644 --- a/tests/auto_type_annotate_test.py +++ b/tests/auto_type_annotate_test.py @@ -35,6 +35,12 @@ def test_to_mod_src_layout(): assert _to_mod('bar.py', ('.', 'src')) == 'bar' +def test_to_mod_init_files(): + assert _to_mod('a/__init__.py', ('.',)) == 'a' + assert _to_mod('a/__init__b.py', ('.',)) == 'a.__init__b' + assert _to_mod('a/__init__b/c.py', ('.',)) == 'a.__init__b.c' + + def _find_untyped(s): visitor = FindUntyped() visitor.visit_module(_MOD, ast.parse(s))