Skip to content

Commit

Permalink
Use a dict for scope indices
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 23, 2021
1 parent 263b5c8 commit 5265c80
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/_pytest/scope.py
Expand Up @@ -8,7 +8,6 @@
Also this makes the module light to import, as it should.
"""
from enum import Enum
from functools import lru_cache
from typing import Optional
from typing import TYPE_CHECKING

Expand All @@ -29,7 +28,7 @@ class Scope(Enum):

def index(self) -> int:
"""Return this scope index. Smaller numbers indicate higher scopes (Session = 0)."""
return _scope_to_index(self)
return _SCOPE_INDICES[self]

def next(self) -> "Scope":
"""Return the next scope (from top to bottom)."""
Expand Down Expand Up @@ -70,9 +69,5 @@ def from_user(
Scope.Class: Scope.Function,
}


@lru_cache(maxsize=None)
def _scope_to_index(scope: Scope) -> int:
"""Implementation of Scope.index() as a free function so we can cache it."""
scopes = list(Scope)
return scopes.index(scope)
# Maps a scope to its internal index, with Session = 0, Package = 1, and so on.
_SCOPE_INDICES = {s: i for i, s in enumerate(Scope)}

0 comments on commit 5265c80

Please sign in to comment.