Skip to content

Commit a5ec7fd

Browse files
authored
Merge pull request #427 from TTsangSC/scoping-policy-typing-fix
FIX: typing of `ScopingPolicy` members
2 parents 9aea7d8 + c2a6a30 commit a5ec7fd

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Changes
77
``sys.modules['__main__']`` namespace to avoid issues w/e.g. pickling
88
(#423)
99
* CHANGE: Drop support for Python 3.8 and Python 3.9
10+
* FIX: ``ScopingPolicy`` members now type-check properly as instances
11+
thereof -- at least on Python versions (3.11+) where ``enum.StrEnum``
12+
is available (#427)
1013
* FIX: Bytecodes of profiled functions now always labeled to prevent
1114
confusion with non-profiled "twins" (#425)
1215

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)