packaging
packaging copied to clipboard
Improve ergonomics of `Requirement.marker` evaluation
Currently Requirement.marker
is of type Optional[Marker]
, and the following code is needed to check whether a requirement needs to be resolved:
def needs_resolution(r: Requirement) -> bool:
if not r.marker:
return True
return r.marker.evaluate()
which becomes cumbersome quickly. I can think of three ways to improve the interface:
- Allow
Marker('')
that always evaluate to True (similar toSpecifierSet('')
), and use that to indicate no markers instead ofNone
. This could be a problem if people are checking formarker is None
, but I think most of the use cases should also be compatible withevaluate()
. - Similar to 2., but do this with another class
EmptyMarker
. This has most of the same problems as above, and may also be a problem for people doingisinstance(marker, Marker)
. Maybe this can be worked around with__subclasscheck__
? - Introduce a convinience method
Requirement.evaluate_marker()
that encapsulate the above logic. This is the least disruptive change, but could be a bit confusing.