pypa / pypa/setuptools

`pkg_resources.evaluate_marker` ignores the `extra` argument

Open
#2,007 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

Problem:

Trying to evaluate markers like extra == "test" and python_version == "2.7" gives an error:

File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1378, in evaluate_marker
 return marker.evaluate()
 File "/usr/local/lib/python2.7/dist-packages/pkg_resources/_vendor/packaging/markers.py", line 301, in evaluate
 return _evaluate_markers(self._markers, current_environment)
 File "/usr/local/lib/python2.7/dist-packages/pkg_resources/_vendor/packaging/markers.py", line 226, in _evaluate_markers
 lhs_value = _get_env(environment, lhs.value)
 File "/usr/local/lib/python2.7/dist-packages/pkg_resources/_vendor/packaging/markers.py", line 208, in _get_env
 "{0!r} does not exist in evaluation environment.".format(name)
pkg_resources._vendor.packaging.markers.UndefinedEnvironmentName: 'extra' does not exist in evaluation environment.

An example of such marker is in the testpath distribution that jupyter depends on.

Fix:

diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
index 75563f95..db089550 100644
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -1376,7 +1376,7 @@ def evaluate_marker(text, extra=None):
     """
     try:
         marker = packaging.markers.Marker(text)
-        return marker.evaluate()
+        return marker.evaluate({'extra': extra})
     except packaging.markers.InvalidMarker as e:
         raise SyntaxError(e)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pkg_resources/init.py at evaluate_marker and reproduce the reported marker evaluation involving extra and python_version. Confirm the change is covered by the project's relevant tests and that evaluating a marker with an extra no longer raises UndefinedEnvironmentName.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.