packaging icon indicating copy to clipboard operation
packaging copied to clipboard

There is no way to create a SpecifierSet directly from one or more Specifiers

Open pfmoore opened this issue 1 year ago • 1 comments

The SpecifierSet constructor takes a string as input. But if you have a list of Specifier objects, there's no way to make a SpecifierSet from them without converting them back to strings and re-parsing.

Could a classmethod be added to build a specifier set from an iterable of Specifiers? It could be as simple as

@classmethod
def from_specifiers(cls, specs: Iterable[Specifier], prerelease: Optional[bool] = None):
    new_value = cls()
    new_value._specs = specs
    new_value._prereleases = prereleases
    return new_value

I'd be happy to create a PR for this if the idea is acceptable.

pfmoore avatar Jan 18 '24 17:01 pfmoore

On reflection, this probably doesn't need a new classmethod - simply allowing construction from an iterator of Specifier instances should be fine.

I've created https://github.com/pypa/packaging/pull/777 implementing this.

pfmoore avatar Jan 19 '24 10:01 pfmoore

I think this can be closed now that https://github.com/pypa/packaging/pull/777 was merged

edgarrmondragon avatar Sep 05 '24 20:09 edgarrmondragon