Skip to content

Commit 6762eea

Browse files
authored
docs(markers): document & and | operators for combining Marker objects (#1151)
* docs(markers): document & and | operators for combining Marker objects * Add versionadded directive for marker operators Per review feedback from henryiii.
1 parent e892020 commit 6762eea

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

docs/markers.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,34 @@ Usage
4242
>>> markers1 == markers2
4343
True
4444

45+
Combining markers programmatically
46+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
48+
:class:`Marker` objects support the ``&`` (and) and ``|`` (or) operators for
49+
combining markers without manually constructing marker strings:
50+
51+
.. doctest::
52+
53+
>>> from packaging.markers import Marker
54+
>>> py3 = Marker("python_version >= '3'")
55+
>>> linux = Marker("sys_platform == 'linux'")
56+
>>> combined = py3 & linux
57+
>>> str(combined)
58+
'python_version >= "3" and sys_platform == "linux"'
59+
>>> either = py3 | linux
60+
>>> str(either)
61+
'python_version >= "3" or sys_platform == "linux"'
62+
63+
This is equivalent to writing the combined marker string directly but is useful
64+
when building markers dynamically from separate conditions.
65+
66+
.. versionadded:: 26.1
67+
4568

4669
Reference
4770
---------
4871

4972
.. automodule:: packaging.markers
5073
:members:
74+
:special-members: __and__, __or__
5175
:exclude-members: __init__

0 commit comments

Comments
 (0)