warning_types.py

Overview

The [warning_types.py](/projects/286/67431) file defines a structured hierarchy of custom warning classes used throughout the pytest testing framework. These warnings categorize various issues and informational signals encountered during pytest’s operation, such as configuration problems, deprecated features, experimental APIs, and runtime anomalies like unhandled exceptions or resource leaks.

Additionally, this file provides utility constructs to handle warnings that require dynamic message formatting at runtime and a helper function to emit warnings explicitly tied to specific function definitions with accurate source location information.

The main goals of this file are:


Classes

PytestWarning

class PytestWarning(UserWarning)
import warnings
warnings.warn("This is a generic pytest warning", PytestWarning)

PytestAssertRewriteWarning

@final
class PytestAssertRewriteWarning(PytestWarning)
warnings.warn("Assert rewrite issue detected", PytestAssertRewriteWarning)

PytestCacheWarning

@final
class PytestCacheWarning(PytestWarning)

PytestConfigWarning

@final
class PytestConfigWarning(PytestWarning)

PytestCollectionWarning

@final
class PytestCollectionWarning(PytestWarning)

PytestDeprecationWarning

class PytestDeprecationWarning(PytestWarning, DeprecationWarning)

PytestRemovedIn9Warning

class PytestRemovedIn9Warning(PytestDeprecationWarning)

PytestExperimentalApiWarning

@final
class PytestExperimentalApiWarning(PytestWarning, FutureWarning)
warnings.warn(PytestExperimentalApiWarning.simple("my_new_feature"))

PytestReturnNotNoneWarning

@final
class PytestReturnNotNoneWarning(PytestWarning)

PytestUnknownMarkWarning

@final
class PytestUnknownMarkWarning(PytestWarning)

PytestUnraisableExceptionWarning

@final
class PytestUnraisableExceptionWarning(PytestWarning)

PytestUnhandledThreadExceptionWarning

@final
class PytestUnhandledThreadExceptionWarning(PytestWarning)

PytestFDWarning

@final
class PytestFDWarning(PytestWarning)

UnformattedWarning

@final
@dataclasses.dataclass
class UnformattedWarning(Generic[_W]):
uw = UnformattedWarning(PytestExperimentalApiWarning, "{} is experimental")
warn_instance = uw.format(apiname="my_api")
warnings.warn(warn_instance)

Functions

warn_explicit_for

def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
def my_hook():
    pass

warn_explicit_for(my_hook, PytestConfigWarning("Deprecated hook signature"))

Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class PytestWarning {
        <<UserWarning>>
    }
    class PytestAssertRewriteWarning {
        <<final>>
    }
    class PytestCacheWarning {
        <<final>>
    }
    class PytestConfigWarning {
        <<final>>
    }
    class PytestCollectionWarning {
        <<final>>
    }
    class PytestDeprecationWarning {
    }
    class PytestRemovedIn9Warning {
    }
    class PytestExperimentalApiWarning {
        +classmethod simple(apiname: str) PytestExperimentalApiWarning
        <<final>>
    }
    class PytestReturnNotNoneWarning {
        <<final>>
    }
    class PytestUnknownMarkWarning {
        <<final>>
    }
    class PytestUnraisableExceptionWarning {
        <<final>>
    }
    class PytestUnhandledThreadExceptionWarning {
        <<final>>
    }
    class PytestFDWarning {
        <<final>>
    }
    class UnformattedWarning~_W~ {
        -category: type[_W]
        -template: str
        +format(**kwargs) _W
        <<final>>
    }

    PytestAssertRewriteWarning --|> PytestWarning
    PytestCacheWarning --|> PytestWarning
    PytestConfigWarning --|> PytestWarning
    PytestCollectionWarning --|> PytestWarning
    PytestDeprecationWarning --|> PytestWarning
    PytestRemovedIn9Warning --|> PytestDeprecationWarning
    PytestExperimentalApiWarning --|> PytestWarning
    PytestReturnNotNoneWarning --|> PytestWarning
    PytestUnknownMarkWarning --|> PytestWarning
    PytestUnraisableExceptionWarning --|> PytestWarning
    PytestUnhandledThreadExceptionWarning --|> PytestWarning
    PytestFDWarning --|> PytestWarning

Summary

The [warning_types.py](/projects/286/67431) module is a foundational part of pytest’s infrastructure for communicating issues, deprecations, and informational signals to users via Python’s warning system. Its well-organized class hierarchy, support for runtime message formatting, and explicit warning emission facilitate clear, contextual, and maintainable warning handling throughout the pytest ecosystem.