Repository: pytest-dev/pytest
File: src/_pytest/unittest.py (Python)

Code before:
    """Register the TestCaseFunction class as an IReporter if twisted.trial is available."""
    if _get_twisted_version() is not TwistedVersion.NotInstalled:
        from twisted.trial.itrial import IReporter
        from zope.interface import classImplements

        classImplements(TestCaseFunction, IReporter)


class TwistedVersion(Enum):
    """
    The Twisted version installed in the environment.

    We have different workarounds in place for different versions of Twisted.
    """

    # Twisted version 24 or prior.
    Version24 = auto()
    # Twisted version 25 or later.
    Version25 = auto()
    # Twisted version is not available.
    NotInstalled = auto()


def _get_twisted_version() -> TwistedVersion:
    # Check `"twisted.trial.unittest` is in sys.modules to ensure that Twisted is installed
    # being used to run the test suite.
    if "twisted.trial.unittest" not in sys.modules:
        return TwistedVersion.NotInstalled

    import importlib.metadata

    import packaging.version

    version_str = importlib.metadata.version("twisted")
    version = packaging.version.parse(version_str)
    if version.major <= 24:
        return TwistedVersion.Version24
    else:
        return TwistedVersion.Version25


# Name of the attribute in `twisted.python.Failure` instances that stores
# the `sys.exc_info()` tuple.
# See twisted.trial support in `pytest_runtest_protocol`.
TWISTED_RAW_EXCINFO_ATTR = "_twisted_raw_excinfo"


@hookimpl(wrapper=True)
def pytest_runtest_protocol(item: Item) -> Iterator[None]:
    if _get_twisted_version() is TwistedVersion.Version24:
        import twisted.python.failure as ut

Review comment (suggestion):
```suggestion
    # We need to check if "twisted.trial.unittest" is specifically present in sys.modules. 
    # This is because we intend to integrate with Trial only when it's actively running the test suite, but not needed when only other Twisted components are in use.
```

Code after:
    """Register the TestCaseFunction class as an IReporter if twisted.trial is available."""
    if _get_twisted_version() is not TwistedVersion.NotInstalled:
        from twisted.trial.itrial import IReporter
        from zope.interface import classImplements

        classImplements(TestCaseFunction, IReporter)


class TwistedVersion(Enum):
    """
    The Twisted version installed in the environment.

    We have different workarounds in place for different versions of Twisted.
    """

    # Twisted version 24 or prior.
    Version24 = auto()
    # Twisted version 25 or later.
    Version25 = auto()
    # Twisted version is not available.
    NotInstalled = auto()


def _get_twisted_version() -> TwistedVersion:
    # We need to check if "twisted.trial.unittest" is specifically present in sys.modules.
    # This is because we intend to integrate with Trial only when it's actively running
    # the test suite, but not needed when only other Twisted components are in use.
    if "twisted.trial.unittest" not in sys.modules:
        return TwistedVersion.NotInstalled

    import importlib.metadata

    import packaging.version

    version_str = importlib.metadata.version("twisted")
    version = packaging.version.parse(version_str)
    if version.major <= 24:
        return TwistedVersion.Version24
    else:
        return TwistedVersion.Version25


# Name of the attribute in `twisted.python.Failure` instances that stores
# the `sys.exc_info()` tuple.
# See twisted.trial support in `pytest_runtest_protocol`.
TWISTED_RAW_EXCINFO_ATTR = "_twisted_raw_excinfo"


@hookimpl(wrapper=True)
def pytest_runtest_protocol(item: Item) -> Iterator[None]:
    if _get_twisted_version() is TwistedVersion.Version24: