Repository: pantsbuild/pants

Issue:
`pants_requirement` should include the `; python_version<'3'` environment marker
Until pants can run on python 3, the `pants_requirement` should add an environment marker to restict the python environment it applies to. After pants only supports `>=3.5` a change should be made to the environment marker.


Discussion:

Resolving patch:
diff --git a/src/python/pants/backend/python/pants_requirement.py b/src/python/pants/backend/python/pants_requirement.py
--- a/src/python/pants/backend/python/pants_requirement.py
+++ b/src/python/pants/backend/python/pants_requirement.py
@@ -32,7 +32,14 @@ def __call__(self, name=None):
     :param string name: The name to use for the target, defaults to the parent dir name.
     """
     name = name or os.path.basename(self._parse_context.rel_path)
-    requirement = PythonRequirement(requirement='pantsbuild.pants=={}'.format(pants_version()))
+
+    # TODO(John Sirois): Modify to constraint to >=3.5,<4 as part of
+    # https://github.com/pantsbuild/pants/issues/6062
+    env_marker = "python_version>='2.7' and python_version<'3'"
+
+    requirement = PythonRequirement(requirement="pantsbuild.pants=={version} ; {env_marker}"
+                                    .format(version=pants_version(), env_marker=env_marker))
+
     self._parse_context.create_object('python_requirement_library',
                                       name=name,
                                       requirements=[requirement])


Test patch:
diff --git a/tests/python/pants_test/backend/python/test_pants_requirement.py b/tests/python/pants_test/backend/python/test_pants_requirement.py
--- a/tests/python/pants_test/backend/python/test_pants_requirement.py
+++ b/tests/python/pants_test/backend/python/test_pants_requirement.py
@@ -19,9 +19,23 @@ def alias_groups(cls):
 
   def assert_pants_requirement(self, python_requirement_library):
     self.assertIsInstance(python_requirement_library, PythonRequirementLibrary)
-    pants_requirement = PythonRequirement('pantsbuild.pants=={}'.format(pants_version()))
-    self.assertEqual([pants_requirement.requirement],
-                     list(pr.requirement for pr in python_requirement_library.payload.requirements))
+    expected = PythonRequirement('pantsbuild.pants=={}'.format(pants_version()))
+
+    def key(python_requirement):
+      return (python_requirement.requirement.key,
+              python_requirement.requirement.specs,
+              python_requirement.requirement.extras)
+
+    self.assertEqual([key(expected)],
+                     [key(pr) for pr in python_requirement_library.payload.requirements])
+
+    req = list(python_requirement_library.payload.requirements)[0]
+    self.assertIsNotNone(req.requirement.marker)
+    self.assertTrue(req.requirement.marker.evaluate(),
+                    'pants_requirement() should always work in the current test environment')
+    self.assertFalse(req.requirement.marker.evaluate({'python_version': '3.5'}))
+    self.assertFalse(req.requirement.marker.evaluate({'python_version': '2.6'}))
+    self.assertTrue(req.requirement.marker.evaluate({'python_version': '2.7'}))
 
   def test_default_name(self):
     self.add_to_build_file('3rdparty/python/pants', 'pants_requirement()')