Syntax

New in version 0.2.0.

As with coverage.py, lines are marked with comments in the form:

# pragma: no cover

With coverage_pyver_pragma, the comment may be followed with an expression enclosed in parentheses:

# pragma: no cover (<=py38 and !Windows)

Each expression consists of one or more tags (VERSION_TAG, PLATFORM_TAG or IMPLEMENTATION_TAG). The tags can be joined with the keywords AND, OR and NOT, with the exclamation mark ! implying NOT. Parentheses can be used to group sub expressions. A series of tags without keywords between them are evaluated with AND.

VERSION_TAG

A VERSION_TAG comprises an optional comparator (one of <=, <, >=, >), a version specifier in the form pyXX, and an optional + to indicate >=.

Example:

<=py36
>=py37
<py38
>py27
py34+  # equivalent to >=py34
PLATFORM_TAG

A PLATFORM_TAG comprises a single word which will be compared (ignoring case) with the output of platform.system().

Example:

Windows
Linux
Darwin  # macOS
Java

If the current platform cannot be determined all strings are treated as True.

IMPLEMENTATION_TAG

An IMPLEMENTATION_TAG comprises a single word which will be compared (ignoring case) with the output of platform.python_implementation().

Example:

CPython
PyPy
IronPython
Jython

Examples

Ignore if the Python version is less than or equal to 3.7:

# pragma: no cover (<=py37)

Ignore if running on Python 3.9:

# pragma: no cover (py39)

Ignore if the Python version is greater than 3.6 and it’s not running on PyPy:

# pragma: no cover (>py36 and !PyPy)

Ignore if the Python version is less than 3.8 and it’s running on Windows:

# pragma: no cover (Windows and <py38)

Ignore when not running on macOS (Darwin):

# pragma: no cover (!Darwin)

Ignore when not running on CPython:

# pragma: no cover (!CPython)

API Reference

Data:

GRAMMAR([name])

The coverage_pyver_pragma expression grammar.

Classes:

ImplementationTag(tokens)

Represents an IMPLEMENTATION_TAG in the expression grammar.

LogicalAND(tokens)

Represents the AND logical operator.

LogicalNOT(tokens)

Represents the NOT / ! logical operator.

LogicalOR(tokens)

Represents the OR logical operator.

LogicalOp(tokens)

Represents a logical operator (AND, OR, and NOT / !).

PlatformTag(tokens)

Represents a PLATFORM_TAG in the expression grammar.

VersionTag(tokens)

Represents a VERSION_TAG in the expression grammar.

GRAMMAR(name=None)

Type:    ParserElement

The coverage_pyver_pragma expression grammar.

This can be used to parse an expression outside of the coverage context.

class ImplementationTag(tokens: pyparsing.results.ParseResults)[source]

Bases: str

Represents an IMPLEMENTATION_TAG in the expression grammar.

An IMPLEMENTATION_TAG comprises a single word which will be compared (ignoring case) with the output of platform.python_implementation().

Examples:

CPython
PyPy
IronPython
Jython
Parameters

tokens

__repr__()[source]

Return a string representation of the ImplementationTag.

Return type

str

class LogicalAND(tokens)[source]

Bases: LogicalOp

Represents the AND logical operator.

Parameters

tokens (ParseResults)

class LogicalNOT(tokens)[source]

Bases: LogicalOp

Represents the NOT / ! logical operator.

Parameters

tokens (ParseResults)

class LogicalOR(tokens)[source]

Bases: LogicalOp

Represents the OR logical operator.

Parameters

tokens (ParseResults)

class LogicalOp(tokens)[source]

Bases: object

Represents a logical operator (AND, OR, and NOT / !).

Parameters

tokens (ParseResults)

__format__(format_spec)[source]

Default object formatter.

Return type

str

__getitem__(item)[source]

Return self[key].

__repr__()[source]

Return a string representation of the LogicalOp.

Return type

str

__str__()[source]

Return str(self).

Return type

str

class PlatformTag(tokens: pyparsing.results.ParseResults)[source]

Bases: str

Represents a PLATFORM_TAG in the expression grammar.

A PLATFORM_TAG comprises a single word which will be compared (ignoring case) with the output of platform.system().

Examples:

Windows
Linux
Darwin  # macOS
Java

If the current platform cannot be determined all strings are treated as True.

Parameters

tokens

__repr__()[source]

Return a string representation of the PlatformTag.

Return type

str

class VersionTag(tokens)[source]

Bases: SpecifierSet

Represents a VERSION_TAG in the expression grammar.

A VERSION_TAG comprises an optional comparator (one of <=, <, >=, >), a version specifier in the form pyXX, and an optional + to indicate >=.

Examples:

<=py36
>=py37
<py38
>py27
py34+
Parameters

tokens (ParseResults)

__repr__()[source]

Return a string representation of the VersionTag.

Return type

str