Skip to content

Commit 242fc57

Browse files
committed
Implementing fix suggested in #426
CHANGELOG.rst Added entry line_profiler/line_profiler.py::LineProfiler._add_namespace(...) Removed unnecessary `typing.cast()` in default arguments line_profiler/line_profiler_utils.py _StrEnum New alias which resolves: - Statically, always to `enum.StrEnum` - At the runtime, also thereto if available, and to `_StrEnumBase` otherwise StringEnum Now always "statically" inheriting from `_StrEnum` to make things less confusing to the type-checker
1 parent 3dd2008 commit 242fc57

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Changes
66
* FIX: Make sure that the profiled code is run in the
77
``sys.modules['__main__']`` namespace to avoid issues w/e.g. pickling
88
(#423)
9+
* FIX: ``ScopingPolicy`` members now type-check properly as instances thereof
10+
(#427)
911

1012

1113
5.0.2

line_profiler/line_profiler.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,15 +600,9 @@ def _add_namespace(
600600
namespace: type | types.ModuleType,
601601
*,
602602
seen: set[int] | None = None,
603-
func_scoping_policy: ScopingPolicy = cast(
604-
ScopingPolicy, ScopingPolicy.NONE
605-
),
606-
class_scoping_policy: ScopingPolicy = cast(
607-
ScopingPolicy, ScopingPolicy.NONE
608-
),
609-
module_scoping_policy: ScopingPolicy = cast(
610-
ScopingPolicy, ScopingPolicy.NONE
611-
),
603+
func_scoping_policy: ScopingPolicy = ScopingPolicy.NONE,
604+
class_scoping_policy: ScopingPolicy = ScopingPolicy.NONE,
605+
module_scoping_policy: ScopingPolicy = ScopingPolicy.NONE,
612606
wrap: bool = False,
613607
name: str | None = None,
614608
) -> int:

line_profiler/line_profiler_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ def __str__(self) -> str:
4646
return self.value
4747

4848

49-
class StringEnum(getattr(enum, 'StrEnum', _StrEnumBase)): # type: ignore[misc]
49+
try:
50+
from enum import StrEnum as _StrEnum
51+
except ImportError:
52+
if not typing.TYPE_CHECKING: # Don't confuse the typechecker
53+
_StrEnum = _StrEnumBase
54+
55+
56+
class StringEnum(_StrEnum):
5057
"""
5158
Convenience wrapper around :py:class:`enum.StrEnum`.
5259

0 commit comments

Comments
 (0)