# The 'extra' environment marker and its operators

**URL:** <https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976>\
**Category:** Packaging\
**Created:** [August 15, 2020, 6:32am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976 "2020-08-15T06:32:57Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 15, 2020, 6:32am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/1 "2020-08-15T06:32:57Z")

</div>

Related: [Pip wheel install doesn't handle extra marker. · Issue #8469 · pypa/pip · GitHub](https://github.com/pypa/pip/issues/8469)

Currently it is possible to supply the following in `METADATA`:

```http
Name: a
Version: 1.0
Provides-Extra: new-b
Requires-Dist: b >= 2; extra == 'new-b'
Requires-Dist: b < 2; extra != 'new-b'

```

While this logically makes sense, it seems to go against the original intention of the extras feature, i.e. to _add_ optional dependencies to a package. And indeed, none of the current installer implementations handle this correctly. I also could not find similar things in other packaging ecosystems—all equivalents to Python package extras only allow a package to include more specifications, not remove from the base requirements.

My proposal is to amend [PEP 508](https://www.python.org/dev/peps/pep-0508) to explicitly special-case `extra`, and disallow all operators other than `==`. This would free up installers from dealing with convoluted edge cases that rarely anyone really ever thought through, and likely make the specification fit better with how extra is originally designed.

* * *

The only relevent description in PEP 508 is

> Comparisons in marker expressions are typed by the comparison operator. The `<marker_op>` operators that are not in `<version_cmp>` perform the same as they do for strings in Python.

I don’t even want to think about what `extra >=` should mean 😛

[PEP 426](https://www.python.org/dev/peps/pep-0426/) described extras in more detail (saying they’re for “optional dependencies”). But that PEP was withdrawn, and did not clarify what optional dependencies mean either.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 15, 2020, 8:27am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/2 "2020-08-15T08:27:06Z")

</div>

> [@uranusjr](#):
>
> I also could not find similar things in other packaging ecosystems—all equivalents to Python package extras only allow a package to include more specifications, not remove from the base requirements.

Python did a lot of things first, I don’t find this line of reasoning to be relevant.

But the rest is. Though how is excluding requirements based on extras so much different from excluding based on platform or Python version?

String ordering is fraught with peril, so I’m not opposed to just ruling out those comparisons, but have we actually seen issues? Or are we just simplifying specs because we can’t imagine the uses right now?

(Allowing the extra marker to use `extra in {set}` seems interesting to me, though guessing you really didn’t want to expand the syntax right now 😉)

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 15, 2020, 10:43am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/3 "2020-08-15T10:43:37Z")

</div>

The discussion [here](https://discuss.python.org/t/adding-a-default-extra-require-environment/4898/46) on “default extras” is probably relevant, too. But it’s essentially arguing for some sort of “extras that remove dependencies” feature, and one of the reasons I find the discussion confusing is _precisely_ that “extras removing things” feels weird to me.

So +1 on this proposal specifically. Also I’d be in favour of a more general principle that “extras only add stuff”. People looking for a more general “manipulate dependencies at install time” facility should come up with a design that _isn’t_ constrained by how extras currently work. Such a design may well (probably should) make extras obsolete. @steve.dower’s [selector packages](https://discuss.python.org/t/idea-selector-packages/4463) idea may be worth reviewing in that context.

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 15, 2020, 11:42am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/4 "2020-08-15T11:42:14Z")

</div>

> [@steve.dower](#):
>
> But the rest is. Though how is excluding requirements based on extras so much different from excluding based on platform or Python version?

The difference is that platform and Python version can’t change in a dependency graph, but extras do. This causes troubles to dependency resolution. For example:

- `A` is defined as above
- `B` has two versions 1.0 and 2.0, neither with dependencies
- `C` depends on `A`

What should `pip install C A[new-B]` do? Select `B >= 2` (since that’s the variant wanted by the user)? How does the resolver know if `C` depends on `A` without extras because it does not care about `B`, or specifically wants `B < 2`? The logic gets more delicate if there’s a `D` depending on `A` (no extras), and conditionals that combine multiple extra markers.

> [@steve.dower](#):
>
> (Allowing the extra marker to use `extra in {set}` seems interesting to me, though guessing you really didn’t want to expand the syntax right now 😉)

I agree, and it’s essentially a good short hand to `extra == ... or extra == ...`. But this should be a separate discussion since PEP 508 does not support the set syntax on the right side anyway.

(This actually brings up another issue—should `extra in 'foobarrex'` evaluate to true on `pip install package[rr]`? Does it right now? Gosh.)

> [@pf\_moore](#):
>
> The discussion here on “default extras” is probably relevant, too. But it’s essentially arguing for some sort of “extras that remove dependencies” feature, and one of the reasons I find the discussion confusing is _precisely_ that “extras removing things” feels weird to me.

Personally I think about the default extra thing not as extras removing thing, but extra groups being activated automatically. So `pip install package` implies `package[default]`, and `package[other]` implies `package[default,other]`. I have no idea whether this is also what others have in mind, and nobody really clarifies how exactly how they want the thing be designed thus far, which is why I have largely stayed out of that topic (except pushing for a PEP draft so I can understand what exactly people are thinking).

---

<div class="post-metadata">

**Author:** ![dustin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dustin/32/149_2.png) [@dustin](https://discuss.python.org/u/dustin)\
**Post date:** [August 15, 2020, 3:23pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/5 "2020-08-15T15:23:59Z")

</div>

> [@uranusjr](#):
>
> Personally I think about the default extra thing not as extras removing thing, but extra groups being activated automatically. So `pip install package` implies `package[default]` , and `package[other]` implies `package[default,other]` .

I think this is almost right. It’s actually:

- `pip install package` implies `package[(install_requires),default]` ,
- `pip install package[other]` implies `package[(install_requires),other]` .

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 15, 2020, 3:31pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/6 "2020-08-15T15:31:11Z")

</div>

It makes sense as well, thanks for the clarification. In that case, the default extra proposal would also be a way to offer the same functionality asked for in the linked pip issue. Nice 🙂

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 16, 2020, 4:45pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/7 "2020-08-16T16:45:08Z")

</div>

> [@uranusjr](#):
>
> The difference is that platform and Python version can’t change in a dependency graph, but extras do. This causes troubles to dependency resolution. For example:
> 
> - `A` is defined as above
> - `B` has two versions 1.0 and 2.0, neither with dependencies
> - `C` depends on `A`
> 
> What should `pip install C A[new-B]` do?

Um, whatever it would normally do…? That example isn’t about trying to omit a requirement, and the rules for resolution seem like they’d be the same as any other version conflict, with the added annoyance that you need to download the full metadata to know what the extra means.

And I’d assume that if a A[extra] excludes a requirement, but B-\>A includes it, you’ll get the requirement. Because that’s essentially the same as when you require two different packages - there’s no conflict here because only one package has the requirement.

(Another case for selector packages: inter-package dependencies don’t have to transitively pass along options that can be queried from the OS, because they all just depend on the selector and it gets resolved once to provide extra requirements).

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 16, 2020, 6:14pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/8 "2020-08-16T18:14:28Z")

</div>

> [@steve.dower](#):
>
> Um, whatever it would normally do…?

C depends on A (without extra new-B, so transitively depends on B \< 2) but the user also requests A[new-B] which depends on B \>= 2. So I guess fail with an error about inconsistent requirements?

But whether that’s what people writing something like this would _expect_, is something I’m much less sure of. And whether it’s what pip does, I’m also not sure of. Once again, the point here is that the semantics of extras are not very clear, and not written up anywhere. But I’ve said this before, so I won’t go on about it again.

I’m in favour of disallowing != comparisons for extras because they are potentially confusing, likely to result in people guessing wrongly about how they will behave, and there’s no obvious use case. Not because we can’t trace through the logic and claim a justification for a particular behaviour…

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 16, 2020, 6:24pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/9 "2020-08-16T18:24:16Z")

</div>

> [@pf\_moore](#):
>
> I’m in favour of disallowing != comparisons for extras because they are potentially confusing, likely to result in people guessing wrongly about how they will behave, and there’s no obvious use case.

Any kind of “slim” option could use this (as was asked in another thread about “how do I default to shipping a command-line tool but optionally just ship the library?”). And since multiple extras are allowed, you’ll force people into even worse logic in some cases.

We can (and probably should) discourage _version_ selection via extras, which is what the first example is actually doing wrong, but I don’t see any valid reason to be worried about the != comparison.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 16, 2020, 6:30pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/10 "2020-08-16T18:30:32Z")

</div>

To be honest, I’m only against != in the broad sense of not liking extras because they are not properly standardised. The more people find “clever” ways to use them, the harder any future standardisation effort becomes, because backward compatibility gets harder.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 16, 2020, 6:32pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/11 "2020-08-16T18:32:07Z")

</div>

If we were going to standardise them, wouldn’t it make the most sense to work it into the environment marker system anyway? Otherwise we’re inventing a brand new system, which would likely require deprecation and transition and all that stuff anyway…

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 17, 2020, 2:13am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/12 "2020-08-17T02:13:05Z")

</div>

> [@steve.dower](#):
>
> Um, whatever it would normally do…? That example isn’t about trying to omit a requirement, and the rules for resolution seem like they’d be the same as any other version conflict, with the added annoyance that you need to download the full metadata to know what the extra means.

What _is_ normal? This is a serious question. As Paul have pointed out, extras are so under-specified that different package users and packagers have different, conflicting ideas what they are exactly. They come complaining when PyPA tools do not do what they expect, and there’s no way to tell them what’s wrong or right because there are absolutely no rules. Hell, even PyPA tools do things differently within themselves. To untangle this, PyPA need to write down those semantics (what extras are) to the extras feature, and come up rules (what packagers and users should expect) from those semantics.

Stepping back a little, this makes me reflect on one of the most troubling difficulties I feel in Python packaging. Many times the response you get when trying to explain the rules to people is “eh, that’s way too convoluted, why don’t you just do it normally,” while those rules are exactly the necessary part to define “normal.”

The two main “schools” to intrepret extras are either to define them as _additional dependency requirements added on to a package_, and _selector for multiple dependency requirement sets_. In the former interpretation, each package has a base set of requirements, and each extra adds a group of additional dependencies to the package. The latter one, on the other hand, treats `package` and `package[extra]` as two different identities that share the same packaged code, and thus can have different sets of requirements.

Difference between the two schools emerge when you try to merge extras, i.e. when a dependency graph requires both `package[extra-a]` and `package[extra-b]`. The add-on school wants the resolver to perform a union (i.e. `package[extra-a, extra-b]`); the selector school wants them to either conflict, or resolved via additional rules (e.g. either `package[extra-a]` or `package[extra-b]` should be chosen to represent `package` depending on which is specified by the user). And there’s no way to tell which of them is right or wrong since there’s no definition at all, and, again, even PyPA tools do different things within themselves.

I am personally okay with either of the interpretations, as long as there is one. But we need to have that conversation, otherwise extras will continue to be this thing that everyone understands differently but no tools implement “correctly” (read: matching users’ expectation). The proposal in the top post is a way to resolve the interpretations by proposing a hard concrete rule, and let expectations be derived. The proposal can very well be rejected, but only if we agree on an alternative one.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 17, 2020, 8:26am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/13 "2020-08-17T08:26:20Z")

</div>

Thanks for the extra rationale, that’s helpful.

I guess the understanding boils down to extra meaning “extra requirement” vs “extra marker”. It seems like the former is more likely to be the original intention, so there’s a solid case for setting that as the definition and breaking anyone who’s misused it since. Though it’s also easy to describe additional requirements in terms of extra environment markers without losing the more complex functionality.

It’s a general rule in language design that users will always find more complex ways to combine your simple primitives. In many ways, that’s the entire story of Python. It would be nice to go with that rather than resist it, but sometimes a line ought to be drawn for technical, educational or compatibility reasons.

I like the generality of environment markers (with the extra-requires section being defined as adding install requirements with those markers), and unless we drastically rework how packages handle machine-specific dependencies I imagine we’ll see more complexity here.

I don’t see any specification for how multiple extras are handled by markers, so that seems like fertile ground for an update. Assuming extras are passed through the checks separately (seems logical), the not equals check isn’t worth much, and if they’re passed all at once then string comparisons are useless.

I have a couple of ideas for how this could be defined, but they’re based on language design principles and what’s in PEP 508 already, so I’d rather hear from those of you directly impacted by the user feedback. (Though I do feel comfortable pushing back against “they’re using it wrong” kind of arguments, since we don’t have a clear idea of right or wrong here 😉)

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 17, 2020, 9:21am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/14 "2020-08-17T09:21:17Z")

</div>

> [@steve.dower](#):
>
> It’s a general rule in language design that users will always find more complex ways to combine your simple primitives. In many ways, that’s the entire story of Python. It would be nice to go with that rather than resist it, but sometimes a line ought to be drawn for technical, educational or compatibility reasons.

In practice, I strongly believe that simple primitives _with well-defined semantics_ can be usefully and powerfully composed in ways that the original designers may not have thought of¹. And this is an incredibly good thing, and we should embrace it. The problem with extras is that we don’t have the “well-defined semantics” part of the equation. Adding that after the fact without invalidating something that someone has done, is likely to be hard - not least because we don’t have a good means of finding out “what people did with this”.

¹ My pure mathematician background is showing here 🙂

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 21, 2020, 12:45am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/15 "2020-08-21T00:45:59Z")

</div>

> [@steve.dower](#):
>
> I don’t see any specification for how multiple extras are handled by markers, so that seems like fertile ground for an update. Assuming extras are passed through the checks separately (seems logical), the not equals check isn’t worth much, and if they’re passed all at once then string comparisons are useless.

The current implementation ([`InstallRequirement.match_markers()`](https://github.com/pypa/pip/blob/e1d489fce65fc7d908b5807b2e58c0c073237fec/src/pip/_internal/req/req_install.py#L280-L291), which calls into [`packaging.markers`](https://github.com/pypa/packaging/blob/39a70cce69d9b08cc4d02b225114d556d5b59ada/packaging/markers.py#L314-L328)) is to evaluate each extra against the marker separately and then combine them. So the dependencies of `package[extra1,extra2]` would return the union of three marker evaluations:

1. `marker.evaluate({'extra': ''})`
2. `marker.evaluate({'extra': 'extra1'})`
3. `marker.evaluate({'extra': 'extra2'})`

The `!=` operator breaks this logic (all inequity operators do) since `dependency; extra != 'extra1'` would evaluate to true for `evaluate({'extra': ''})` and `evalute({'extra': 'extra2'})` and apprear as a dependency, even though `extra1` is specified.

I am not aware how pip implemented this prior to PEP 508, and it might as well be that pip’s current implementation is wrong and should be fixed. I find it unlikely, however, since `packaging.markers` does not offer a reasonable interface to compare multiple `extra` values to a marker all at once.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 21, 2020, 9:04am UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/16 "2020-08-21T09:04:50Z")

</div>

> [@uranusjr](#):
>
> I am not aware how pip implemented this prior to PEP 508, and it might as well be that pip’s current implementation is wrong and should be fixed. I find it unlikely, however, since `packaging.markers` does not offer a reasonable interface to compare multiple `extra` values to a marker all at once.

I suspect you’re imagining that more design went into the current implementation than actually happened 😉

The good news is that because it’s essentially useless, nobody is using it right now and it can be changed. So if we wanted to define semantics for handling multiple extras (in PEP 508), that’s not going to break the world.

There’s about three discussions going on right now in this area, and to me it seems like there’s legitimate value in improving this, so I think we should see how those turn out before committing to removing or changing anything.

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [August 21, 2020, 9:32pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/17 "2020-08-21T21:32:58Z")

</div>

I was thinking about how the _pass extras all at once_ logic can be implemented in `packaging`, and suddenly realised I’ve been feeling weird with the idea because the `==` expression does not make sense in the first place. Currently the extra marker (`extra == 'some_extra'`) kind of implies one extra is passed. So how should it be evaluated for `package[some_extra, another_extra]`? The only implementation that satisfies the expression I can think of would be something like

```python
class Extra:
    def __init__ (self, identifiers: Collection[str]):
        self._identifiers = identifiers

    def __eq__ (self, value: str) -> bool:
        return value in self._identifiers

```

But that’s obviously a very unintuitive definition, and might cause more confusions down the road. I’m not sure what to make of this, maybe it means we should go with the current _do string comparison separately and union_ approach (in which the `==` operator makes sense), or something else.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [August 21, 2020, 10:19pm UTC](https://discuss.python.org/t/the-extra-environment-marker-and-its-operators/4976/18 "2020-08-21T22:19:18Z")

</div>

When I mentioned earlier I had a few ideas that could work, this was my favourite one (with the rest fleshed out). The nice part is that it just defines the interface of the extras value, which is currently undefined, and we can keep it compatible with most existing use.

Still not sure we’re ready to go for anything at all yet though, based on the other threads on this topic that have been active today.
