1515import sys
1616import tempfile
1717import warnings
18+ from collections .abc import Iterator
1819from contextlib import contextmanager
1920from pathlib import Path
20- from typing import Any , Iterator , Literal , Optional , overload
21+ from typing import Any , overload
2122
2223import platformdirs
2324
@@ -43,10 +44,10 @@ def envset(name: str, default: bool = False) -> bool: ...
4344
4445
4546@overload
46- def envset (name : str , default : Literal [ None ] ) -> Optional [ bool ] : ...
47+ def envset (name : str , default : None ) -> bool | None : ...
4748
4849
49- def envset (name : str , default : Optional [ bool ] = False ) -> Optional [ bool ] :
50+ def envset (name : str , default : bool | None = False ) -> bool | None :
5051 """Return the boolean value of a given environment variable.
5152
5253 An environment variable is considered set if it is assigned to a value
@@ -182,12 +183,14 @@ def jupyter_data_dir() -> str:
182183
183184 if sys .platform == "darwin" :
184185 return str (Path (home , "Library" , "Jupyter" ))
186+ # Bug in mypy which thinks it's unreachable: https://github.com/python/mypy/issues/10773
185187 if sys .platform == "win32" :
186188 appdata = os .environ .get ("APPDATA" , None )
187189 if appdata :
188190 return str (Path (appdata , "jupyter" ).resolve ())
189191 return pjoin (jupyter_config_dir (), "data" )
190192 # Linux, non-OS X Unix, AIX, etc.
193+ # Bug in mypy which thinks it's unreachable: https://github.com/python/mypy/issues/10773
191194 xdg = env .get ("XDG_DATA_HOME" , None )
192195 if not xdg :
193196 xdg = pjoin (home , ".local" , "share" )
@@ -303,7 +306,7 @@ def jupyter_path(*subdirs: str) -> list[str]:
303306 if site .ENABLE_USER_SITE :
304307 # Check if site.getuserbase() exists to be compatible with virtualenv,
305308 # which often does not have this method.
306- userbase : Optional [ str ]
309+ userbase : str | None
307310 userbase = site .getuserbase () if hasattr (site , "getuserbase" ) else site .USER_BASE
308311
309312 if userbase :
@@ -400,7 +403,7 @@ def jupyter_config_path() -> list[str]:
400403 # Next is environment or user, depending on the JUPYTER_PREFER_ENV_PATH flag
401404 user = [jupyter_config_dir ()]
402405 if site .ENABLE_USER_SITE :
403- userbase : Optional [ str ]
406+ userbase : str | None
404407 # Check if site.getuserbase() exists to be compatible with virtualenv,
405408 # which often does not have this method.
406409 userbase = site .getuserbase () if hasattr (site , "getuserbase" ) else site .USER_BASE
@@ -442,7 +445,7 @@ def exists(path: str) -> bool:
442445 return True
443446
444447
445- def is_file_hidden_win (abs_path : str , stat_res : Optional [ Any ] = None ) -> bool :
448+ def is_file_hidden_win (abs_path : str , stat_res : Any | None = None ) -> bool :
446449 """Is a file hidden?
447450
448451 This only checks the file itself; it should be called in combination with
@@ -487,7 +490,7 @@ def is_file_hidden_win(abs_path: str, stat_res: Optional[Any] = None) -> bool:
487490 return False
488491
489492
490- def is_file_hidden_posix (abs_path : str , stat_res : Optional [ Any ] = None ) -> bool :
493+ def is_file_hidden_posix (abs_path : str , stat_res : Any | None = None ) -> bool :
491494 """Is a file hidden?
492495
493496 This only checks the file itself; it should be called in combination with
@@ -602,12 +605,12 @@ def win32_restrict_file_to_user(fname: str) -> None:
602605 The path to the file to secure
603606 """
604607 try :
605- import win32api
608+ import win32api # noqa: PLC0415
606609 except ImportError :
607610 return _win32_restrict_file_to_user_ctypes (fname )
608611
609- import ntsecuritycon as con
610- import win32security
612+ import ntsecuritycon as con # noqa: PLC0415
613+ import win32security # noqa: PLC0415
611614
612615 # everyone, _domain, _type = win32security.LookupAccountName("", "Everyone")
613616 admins = win32security .CreateWellKnownSid (win32security .WinBuiltinAdministratorsSid )
@@ -646,8 +649,8 @@ def _win32_restrict_file_to_user_ctypes(fname: str) -> None:
646649 fname : unicode
647650 The path to the file to secure
648651 """
649- import ctypes
650- from ctypes import wintypes
652+ import ctypes # noqa: PLC0415
653+ from ctypes import wintypes # noqa: PLC0415
651654
652655 advapi32 = ctypes .WinDLL ("advapi32" , use_last_error = True ) # type:ignore[attr-defined]
653656 secur32 = ctypes .WinDLL ("secur32" , use_last_error = True ) # type:ignore[attr-defined]
0 commit comments