refactor(pypi): evaluate extras and dependency_groups as sets

The `extras` and `dependency_groups` variables of the lock file
markers are sets of names, but they were handled as strings, i.e.
`"fo" in extras` would be true for the `foo` extra.

Now their value in the `env` is a list of normalized names and the
markers can only test the membership with `<string> in <variable>` or
`<string> not in <variable>`. Any other usage is an invalid marker,
which is stricter than `packaging`, where e.g. `"foo" == extras` is
false. The cases for the conformance tests are generated with
`packaging` like for the other variables.

Nothing sets these variables in the `env` yet, so this does not change
how the requirements are evaluated.
diff --git a/python/private/pypi/pep508_env.bzl b/python/private/pypi/pep508_env.bzl
index 9fe9dca..b616397 100644
--- a/python/private/pypi/pep508_env.bzl
+++ b/python/private/pypi/pep508_env.bzl
@@ -219,7 +219,9 @@
         # Per-variable normalization functions. Each entry maps a marker
         # variable name to a function (value) -> normalized_value.
         "_aliases": {
+            "dependency_groups": normalize_name,
             "extra": normalize_name,
+            "extras": normalize_name,
             "platform_machine": lambda x: platform_machine_aliases.get(x, x),
         },
     }
diff --git a/python/private/pypi/pep508_evaluate.bzl b/python/private/pypi/pep508_evaluate.bzl
index c5451d2..06f200d 100644
--- a/python/private/pypi/pep508_evaluate.bzl
+++ b/python/private/pypi/pep508_evaluate.bzl
@@ -39,6 +39,10 @@
     "sys_platform": False,
 }
 
+# The variables that are sets of names, which can only be used as
+# `<string> in <variable>` or `<string> not in <variable>`.
+_SET_VARIABLES = ["dependency_groups", "extras"]
+
 # The deprecated names that are still found in the metadata of old packages.
 _VARIABLE_ALIASES = {
     "os.name": "os_name",
@@ -253,6 +257,8 @@
     name = _VARIABLE_ALIASES.get(name, name)
     if name not in _VARIABLES:
         return None, "Unknown variable '{}'".format(name)
+    elif name in _SET_VARIABLES and not (value_is_left and op in ["in", "not in"]):
+        return None, "Expected '<string> in {0}' or '<string> not in {0}' at '{1}'".format(name, " ".join(tokens))
 
     is_version = _VARIABLES[name]
     return struct(
@@ -273,10 +279,12 @@
     Args:
         marker: {type}`str` The marker to evaluate or the value returned by
             {obj}`parse`.
-        env: {type}`dict[str, str]` The values of the variables. If it has
-            the `_aliases` key, then its value is a dict of functions to
-            normalize the strings that the variables are compared with, which
-            is how e.g. `platform_machine == "arm64"` is true for `aarch64`.
+        env: {type}`dict[str, str | list[str]]` The values of the variables,
+            which are lists of normalized names for `extras` and
+            `dependency_groups`. If it has the `_aliases` key, then its value
+            is a dict of functions to normalize the strings that the
+            variables are compared with, which is how e.g.
+            `platform_machine == "arm64"` is true for `aarch64`.
         strict: {type}`bool` Whether to fail if a variable is not in the
             `env`. If it is `False`, then the expressions with such variables
             are not evaluated.
diff --git a/tests/pypi/pep508/evaluate_tests.bzl b/tests/pypi/pep508/evaluate_tests.bzl
index f737c63..24764a9 100644
--- a/tests/pypi/pep508/evaluate_tests.bzl
+++ b/tests/pypi/pep508/evaluate_tests.bzl
@@ -107,6 +107,13 @@
         "os_name == posix",
         "'posix' == 'posix'",
         "unknown_variable == 'posix'",
+        # The variables that are sets only support the membership tests.
+        "extras == 'foo'",
+        "'foo' == extras",
+        "'foo' >= extras",
+        "extras in 'foo'",
+        "dependency_groups not in 'foo'",
+        "'foo' != dependency_groups",
         # The variables that are not in the spec are invalid even if they are in the env.
         "in_env == 'posix'",
     ]:
@@ -321,6 +328,29 @@
 
 _tests.append(_evaluate_partial_only_extra)
 
+def _evaluate_set_variables_tests(env):
+    values = {
+        "dependency_groups": [],
+        "extras": ["foo", "bar"],
+        "_aliases": {"extras": lambda x: x.lower()},
+    }
+    for input, want in {
+        "'foo' in extras": True,
+        "'FOO' in extras": True,
+        "'fo' in extras": False,
+        "'foo' not in extras": False,
+        "'baz' not in extras": True,
+        "'foo' in extras and 'bar' in extras": True,
+        "'foo' in extras and 'baz' in extras": False,
+        "'foo' in dependency_groups": False,
+        "'foo' not in dependency_groups": True,
+    }.items():  # buildifier: @unsorted-dict-items
+        _check_evaluate(env, input, want, values)
+
+    _check_evaluate(env, "'foo' in extras", '"foo" in extras', {}, strict = False)
+
+_tests.append(_evaluate_set_variables_tests)
+
 def _evaluate_with_aliases(env):
     # When
     for (os, cpu), tests in {
diff --git a/tests/pypi/pep508/packaging_cases.bzl b/tests/pypi/pep508/packaging_cases.bzl
index 8ac7570..a9f0a09 100644
--- a/tests/pypi/pep508/packaging_cases.bzl
+++ b/tests/pypi/pep508/packaging_cases.bzl
@@ -1147,6 +1147,48 @@
     ("python_implementation", "not in", "things @#4 dsfd (((", "TT"),
 ]
 
+# The value of the variables that are sets for the cases below.
+SET_ENV_VALUE = [
+    "foo",
+    "bar-baz",
+]
+
+# The same as the `MARKER_TABLE`, but for the variables that are sets.
+SET_TABLE = [
+    ("extras", "in", "foo", "ET"),
+    ("extras", "in", "Foo", "ET"),
+    ("extras", "in", "fo", "EF"),
+    ("extras", "in", "bar-baz", "ET"),
+    ("extras", "in", "Bar_Baz", "ET"),
+    ("extras", "in", "bar.baz", "ET"),
+    ("extras", "in", "bar", "EF"),
+    ("extras", "in", "", "EF"),
+    ("extras", "not in", "foo", "EF"),
+    ("extras", "not in", "Foo", "EF"),
+    ("extras", "not in", "fo", "ET"),
+    ("extras", "not in", "bar-baz", "EF"),
+    ("extras", "not in", "Bar_Baz", "EF"),
+    ("extras", "not in", "bar.baz", "EF"),
+    ("extras", "not in", "bar", "ET"),
+    ("extras", "not in", "", "ET"),
+    ("dependency_groups", "in", "foo", "ET"),
+    ("dependency_groups", "in", "Foo", "ET"),
+    ("dependency_groups", "in", "fo", "EF"),
+    ("dependency_groups", "in", "bar-baz", "ET"),
+    ("dependency_groups", "in", "Bar_Baz", "ET"),
+    ("dependency_groups", "in", "bar.baz", "ET"),
+    ("dependency_groups", "in", "bar", "EF"),
+    ("dependency_groups", "in", "", "EF"),
+    ("dependency_groups", "not in", "foo", "EF"),
+    ("dependency_groups", "not in", "Foo", "EF"),
+    ("dependency_groups", "not in", "fo", "ET"),
+    ("dependency_groups", "not in", "bar-baz", "EF"),
+    ("dependency_groups", "not in", "Bar_Baz", "EF"),
+    ("dependency_groups", "not in", "bar.baz", "EF"),
+    ("dependency_groups", "not in", "bar", "ET"),
+    ("dependency_groups", "not in", "", "ET"),
+]
+
 # (marker, expected)
 LOGIC_CASES = [
     ("\"arm\" in platform_machine and \"arm\" in platform_machine and platform_machine in \"x86_64 amd64\" and python_full_version < \"3.11.4\" or python_full_version < \"3.11.4\" and \"arm\" in platform_machine and ((python_full_version < \"3.11.4\" and \"posix\" == os_name) or os_name == \"posix\" or os_name == \"posix\" or python_version >= \"3.8\" and python_full_version < \"3.11.4\" or sys_platform != 'linux' and os_name == \"posix\" and \"arm\" in platform_machine or python_full_version < \"3.11.4\") or ((python_version >= \"3.8\" or os_name == \"posix\") or (platform_machine in \"x86_64 amd64\" and python_version >= \"3.8\" and python_full_version < \"3.11.4\" and extra == \"foo\")) and os_name == \"posix\"", True),
diff --git a/tests/pypi/pep508/packaging_conformance_tests.bzl b/tests/pypi/pep508/packaging_conformance_tests.bzl
index 9cda459..9410fe6 100644
--- a/tests/pypi/pep508/packaging_conformance_tests.bzl
+++ b/tests/pypi/pep508/packaging_conformance_tests.bzl
@@ -14,17 +14,23 @@
     "LOGIC_CASES",
     "MARKER_ENV",
     "MARKER_TABLE",
+    "SET_ENV_VALUE",
+    "SET_TABLE",
 )
 
+_SET_VARIABLES = ["dependency_groups", "extras"]
+
 _tests = []
 
 def _env(overrides = {}):
     env = dict(MARKER_ENV) | overrides | {
-        "_aliases": {"extra": normalize_name},
+        "_aliases": {name: normalize_name for name in ["extra"] + _SET_VARIABLES},
     }
 
-    # The extra in the environment is expected to be already normalized.
+    # The names in the environment are expected to be already normalized.
     env["extra"] = normalize_name(env["extra"])
+    for name in _SET_VARIABLES:
+        env[name] = [normalize_name(value) for value in SET_ENV_VALUE]
     return env
 
 def _test_packaging_evaluate(env):
@@ -51,7 +57,7 @@
 def _test_packaging_marker_table(env):
     failures = []
     marker_env = _env()
-    for variable, op, value, results in MARKER_TABLE:
+    for variable, op, value, results in MARKER_TABLE + SET_TABLE:
         for marker, result in [
             ('{} {} "{}"'.format(variable, op, value), results[0]),
             ('"{}" {} {}'.format(value, op, variable), results[1]),
diff --git a/tests/support/pypa_packaging/generate.py b/tests/support/pypa_packaging/generate.py
index 775af79..30db2a2 100644
--- a/tests/support/pypa_packaging/generate.py
+++ b/tests/support/pypa_packaging/generate.py
@@ -110,6 +110,12 @@
     "sys_platform": "linux",
 }
 
+# The variables that are sets, their value in the environment and the values
+# to test the membership of.
+_SET_VARIABLES = ["extras", "dependency_groups"]
+_SET_ENV_VALUE = ["foo", "bar-baz"]
+_SET_VALUES = ["foo", "Foo", "fo", "bar-baz", "Bar_Baz", "bar.baz", "bar", ""]
+
 # The expressions that are combined with `and`, `or` and parenthesis to test
 # the precedence and the grouping.
 _MARKER_ATOMS = [
@@ -290,7 +296,7 @@
     return "({})".format(marker) if rng.random() < 0.5 else marker
 
 
-def _marker_table(variables, operators, values):
+def _marker_table(env, variables, operators, values):
     rows = []
     for variable, op, value in itertools.product(variables, operators, values):
         # The test builds the markers the same way.
@@ -300,7 +306,7 @@
             '{} {} "{}"'.format(variable, op, value),
             '"{}" {} {}'.format(value, op, variable),
         ]:
-            got = _evaluate_marker(marker, _MARKER_ENV)
+            got = _evaluate_marker(marker, env)
             # "E" means that it is an error to evaluate the marker.
             result += "E" if got is None else "T" if got else "F"
         rows.append((variable, op, value, result))
@@ -362,6 +368,7 @@
             _list(
                 "MARKER_TABLE",
                 _marker_table(
+                    _MARKER_ENV,
                     ns["VARIABLES"]
                     + ns["PEP_345_VARIABLES"]
                     + ns["SETUPTOOLS_VARIABLES"],
@@ -372,6 +379,22 @@
                 "# `<variable> <operator> <value>` and `<value> <operator> <variable>`.",
             ),
             _list(
+                "SET_ENV_VALUE",
+                _SET_ENV_VALUE,
+                "The value of the variables that are sets for the cases below.",
+            ),
+            _list(
+                "SET_TABLE",
+                _marker_table(
+                    _MARKER_ENV
+                    | {v: frozenset(_SET_ENV_VALUE) for v in _SET_VARIABLES},
+                    _SET_VARIABLES,
+                    ["in", "not in"],
+                    _SET_VALUES,
+                ),
+                "The same as the `MARKER_TABLE`, but for the variables that are sets.",
+            ),
+            _list(
                 "LOGIC_CASES",
                 sorted(logic.items()),
                 "(marker, expected)",