Given a scope name from the user, return the equivalent Scope enum. Should be used whenever we want to convert a user provided scope name to its enum object. If the scope name is invalid, construct a user friendly message and call pytest.fail.
(
cls, scope_name: ScopeName, descr: str, where: str | None = None
)
| 60 | |
| 61 | @classmethod |
| 62 | def from_user( |
| 63 | cls, scope_name: ScopeName, descr: str, where: str | None = None |
| 64 | ) -> Scope: |
| 65 | """ |
| 66 | Given a scope name from the user, return the equivalent Scope enum. Should be used |
| 67 | whenever we want to convert a user provided scope name to its enum object. |
| 68 | |
| 69 | If the scope name is invalid, construct a user friendly message and call pytest.fail. |
| 70 | """ |
| 71 | from _pytest.outcomes import fail |
| 72 | |
| 73 | try: |
| 74 | # Holding this reference is necessary for mypy at the moment. |
| 75 | scope = Scope(scope_name) |
| 76 | except ValueError: |
| 77 | fail( |
| 78 | "{} {}got an unexpected scope value '{}'".format( |
| 79 | descr, f"from {where} " if where else "", scope_name |
| 80 | ), |
| 81 | pytrace=False, |
| 82 | ) |
| 83 | return scope |
| 84 | |
| 85 | |
| 86 | _ALL_SCOPES = list(Scope) |