diff --git a/src/latexplotlib/_config.py b/src/latexplotlib/_config.py index 716708d..8f3e347 100644 --- a/src/latexplotlib/_config.py +++ b/src/latexplotlib/_config.py @@ -25,7 +25,7 @@ DEFAULT_HEIGHT = 412 -def find_pyproject_toml() -> Path: +def find_pyproject_toml() -> Path | None: pyproject = "pyproject.toml" path = Path().absolute() while path != Path("/"): @@ -33,24 +33,24 @@ def find_pyproject_toml() -> Path: return path / pyproject path = path.parent - msg = "Could not find 'pyproject.toml'" - raise FileNotFoundError(msg) + return None -def find_config_ini() -> Path: - msg = f""" - Configuring latexplotlib via {CONFIGPATH} is being deprecated. Please use - the [tool.latexplotlib] section of the 'pyproject.toml' file instead. +def find_config_ini() -> Path | None: + if CONFIGPATH.exists(): + msg = f""" + Configuring latexplotlib via '{CONFIGPATH}' is being deprecated. Please use + the [tool.latexplotlib] section of the 'pyproject.toml' file instead. If a + 'pyproject.toml' file is present in the current directory or a parent + directory, the configuration at '{CONFIGPATH}' is being ignored. - To silence this warning, please delete the config file {CONFIGPATH} - """ - warnings.warn(msg, DeprecationWarning, stacklevel=2) + To silence this warning, please delete the config file '{CONFIGPATH}'. + """ + warnings.warn(msg, DeprecationWarning, stacklevel=5) - if CONFIGPATH.exists(): return CONFIGPATH - msg = f"No such file: '{CONFIGPATH}'" - raise FileNotFoundError(msg) + return None class Size: @@ -133,13 +133,16 @@ def __str__(self) -> str: return str(f"{self._width}pt, {self._height}pt") -try: - path = find_pyproject_toml() - size = Size.from_pyproject_toml(path) -except FileNotFoundError: - try: - path = find_config_ini() +def get_size() -> Size: + if (path := find_pyproject_toml()) is not None: + # look for config.ini to emit deprecation warning if it exists + _ = find_config_ini() + return Size.from_pyproject_toml(path) + + if (path := find_config_ini()) is not None: + return Size.from_config_ini(path) + + return Size(width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT) + - size = Size.from_config_ini(path) - except FileNotFoundError: - size = Size(width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT) +size = get_size() diff --git a/tests/test_10_config.py b/tests/test_10_config.py index 8fe3308..be389e9 100644 --- a/tests/test_10_config.py +++ b/tests/test_10_config.py @@ -24,8 +24,7 @@ def test_pyproject_exists(self, path): cfg.find_pyproject_toml() def test_pyproject_not_exists(self, path): - with pytest.raises(FileNotFoundError): - cfg.find_pyproject_toml() + assert cfg.find_pyproject_toml() is None class TestFindConfigIni: @@ -45,8 +44,7 @@ def test_configini_exists(self, path): cfg.find_config_ini() def test_configini_not_exists(self, path): - with pytest.warns(DeprecationWarning), pytest.raises(FileNotFoundError): - cfg.find_config_ini() + assert cfg.find_config_ini() is None class TestSize: @@ -127,3 +125,8 @@ def test_from_pyproject_toml_tool_latexplotlib(self, tmp_path): size = cfg.Size.from_pyproject_toml(path) assert size._width == cfg.DEFAULT_WIDTH assert size._height == cfg.DEFAULT_HEIGHT + + +class TestGetSize: + def test_todo(self): + raise NotImplementedError