Skip to content

Commit dc840a3

Browse files
authored
narrow type of envset when it can't be None (#417)
* narrow type of envset when it can't be None it's guaranteed to be bool unless `default=None`, reflect that with overload * interrogate: ignore overloaded functions
1 parent 7a1dbbb commit dc840a3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

jupyter_core/paths.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import warnings
1818
from contextlib import contextmanager
1919
from pathlib import Path
20-
from typing import Any, Iterator, Optional
20+
from typing import Any, Iterator, Literal, Optional, overload
2121

2222
import platformdirs
2323

@@ -38,6 +38,14 @@
3838
UF_HIDDEN = getattr(stat, "UF_HIDDEN", 32768)
3939

4040

41+
@overload
42+
def envset(name: str, default: bool = False) -> bool: ...
43+
44+
45+
@overload
46+
def envset(name: str, default: Literal[None]) -> Optional[bool]: ...
47+
48+
4149
def envset(name: str, default: Optional[bool] = False) -> Optional[bool]:
4250
"""Return the boolean value of a given environment variable.
4351
@@ -58,7 +66,7 @@ def use_platform_dirs() -> bool:
5866
We plan for this to default to False in jupyter_core version 5 and to True
5967
in jupyter_core version 6.
6068
"""
61-
return envset("JUPYTER_PLATFORM_DIRS", False) # type:ignore[return-value]
69+
return envset("JUPYTER_PLATFORM_DIRS", False)
6270

6371

6472
def get_home_dir() -> str:
@@ -103,7 +111,7 @@ def prefer_environment_over_user() -> bool:
103111
"""Determine if environment-level paths should take precedence over user-level paths."""
104112
# If JUPYTER_PREFER_ENV_PATH is defined, that signals user intent, so return its value
105113
if "JUPYTER_PREFER_ENV_PATH" in os.environ:
106-
return envset("JUPYTER_PREFER_ENV_PATH") # type:ignore[return-value]
114+
return envset("JUPYTER_PREFER_ENV_PATH")
107115

108116
# If we are in a Python virtualenv, default to True (see https://docs.python.org/3/library/venv.html#venv-def)
109117
if sys.prefix != sys.base_prefix and _do_i_own(sys.prefix):

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ ignore-semiprivate=true
220220
ignore-property-decorators=true
221221
ignore-nested-functions=true
222222
ignore-nested-classes=true
223+
ignore-overloaded-functions=true
223224
fail-under=100
224225
exclude = ["docs", "tests"]
225226

0 commit comments

Comments
 (0)