@@ -204,6 +204,87 @@ def test_bottom_heavy(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
204204 assert arch .layers == [{"f" , "g" }, {"d" , "e" }, {"a" , "b" , "c" }]
205205 assert arch .excluded == set ()
206206
207+ def test_version_excluded_with_others (
208+ self , tmp_path : Path , monkeypatch : pytest .MonkeyPatch
209+ ):
210+ """_version is excluded even when sharing a layer with other modules.
211+
212+ Ref: https://github.com/usethis-python/usethis-python/issues/1423
213+ """
214+ # Arrange
215+ (tmp_path / "salut" ).mkdir ()
216+ (tmp_path / "salut" / "__init__.py" ).touch ()
217+ (tmp_path / "salut" / "a.py" ).touch ()
218+ (tmp_path / "salut" / "b.py" ).touch ()
219+ (tmp_path / "salut" / "_version.py" ).touch ()
220+
221+ monkeypatch .syspath_prepend (str (tmp_path ))
222+
223+ # Act
224+ with change_cwd (tmp_path ):
225+ graph = _get_graph ("salut" )
226+ arch = _get_module_layered_architecture ("salut" , graph = graph )
227+
228+ # Assert
229+ assert arch .layers == [{"a" , "b" }]
230+ assert arch .excluded == {"_version" }
231+
232+ def test_version_excluded_sole_in_layer (
233+ self , tmp_path : Path , monkeypatch : pytest .MonkeyPatch
234+ ):
235+ """_version is excluded even when it is the sole module in its layer.
236+
237+ The empty layer should be cleaned up.
238+
239+ Ref: https://github.com/usethis-python/usethis-python/issues/1423
240+ """
241+ # Arrange
242+ (tmp_path / "salut" ).mkdir ()
243+ (tmp_path / "salut" / "__init__.py" ).touch ()
244+ (tmp_path / "salut" / "a.py" ).touch ()
245+ (tmp_path / "salut" / "b.py" ).write_text ("""\
246+ import salut.a
247+ """ )
248+ (tmp_path / "salut" / "_version.py" ).touch ()
249+
250+ monkeypatch .syspath_prepend (str (tmp_path ))
251+
252+ # Act
253+ with change_cwd (tmp_path ):
254+ graph = _get_graph ("salut" )
255+ arch = _get_module_layered_architecture ("salut" , graph = graph )
256+
257+ # Assert
258+ assert arch .layers == [{"b" }, {"a" }]
259+ assert arch .excluded == {"_version" }
260+
261+ def test_version_excluded_with_deps (
262+ self , tmp_path : Path , monkeypatch : pytest .MonkeyPatch
263+ ):
264+ """_version is excluded even when other modules import it.
265+
266+ Ref: https://github.com/usethis-python/usethis-python/issues/1423
267+ """
268+ # Arrange
269+ (tmp_path / "salut" ).mkdir ()
270+ (tmp_path / "salut" / "__init__.py" ).touch ()
271+ (tmp_path / "salut" / "a.py" ).write_text ("""\
272+ import salut._version
273+ """ )
274+ (tmp_path / "salut" / "b.py" ).touch ()
275+ (tmp_path / "salut" / "_version.py" ).touch ()
276+
277+ monkeypatch .syspath_prepend (str (tmp_path ))
278+
279+ # Act
280+ with change_cwd (tmp_path ):
281+ graph = _get_graph ("salut" )
282+ arch = _get_module_layered_architecture ("salut" , graph = graph )
283+
284+ # Assert
285+ assert arch .layers == [{"a" }, {"b" }]
286+ assert arch .excluded == {"_version" }
287+
207288
208289class TestGetChildDependencies :
209290 def test_three (self , tmp_path : Path , monkeypatch : pytest .MonkeyPatch ):
0 commit comments