You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
typing.Set (built-in set type) is aliased to collections.abc.Set (abstract set) #264
GenConverter's unstruct_collection_overrides param expects a collections.abc.Set in Python 3.9+ and a typing.Set in Python 3.8 and below. typing.Set is a generic version of the set built-in; the typing module's equivalent of collections.abc.Set is AbstractSet. In 3.8 and below, collections.abc.Set is silently ignored and typing.Set incorrectly applies to all set types. I would expect collections.abc.Set to work on all (supported) Python versions, especially considering that paremetrised overrides are not supported (Set[...] has no effect).
What I Did
fromcollections.abcimportSetconverter=GenConverter(unstruct_collection_overrides={Set: sorted})
converter.unstructure(frozenset(())) # Not converted to a list on Python 3.8
On 3.8 and below, collections.abc.Set doesn't support subscripting (collections.abc.Set[int] is an error) so I don't think it's worth supporting. I believe on those versions the recommendation is to use the typing alternatives.
I think supporting typing.AbstractSet does make sense on those versions. So a collection override for typing.AbstractSet would apply to MutableSet, builtin sets, and frozen sets.
Since typing.AbstractSet was deprecated in 3.9, I don't think it's important to support it on those versions - on 3.9+ we would expect the users to switch to the collections.abc variants.
When I say I don't consider something worth supporting, I'm doing some cost/benefit analysis of the maintenance burden of all these combinations, and as you see there are a lot of them.
So in your case, your snippet (on 3.8) would need to become:
from typing import AbstractSet
converter = GenConverter(unstruct_collection_overrides={AbstractSet: sorted})
converter.unstructure(frozenset(()))
On 3.8 and below, collections.abc.Set doesn't support subscripting (collections.abc.Set[int] is an error)
But collection overrides themselves don't support subscripting, right? GenConverter(unstruct_collection_overrides={collections.abc.Set[int]: sorted}) won't do anything. Arguably, the converter should raise for unsupported overrides. Is there a reason not to support collections.abc.Set across all Python versions? Anyway, either typing.AbstractSet or collections.abc.Set would be fine :)
The docs say this feature isn't supported on versions prior to 3.9 btw, and I remember why not - it's very gnarly to get right across all the versions :/
Activity
Tinche commentedon May 4, 2022
Ooph, let's see, it's a little confusing.
On 3.8 and below,
collections.abc.Setdoesn't support subscripting (collections.abc.Set[int]is an error) so I don't think it's worth supporting. I believe on those versions the recommendation is to use thetypingalternatives.I think supporting
typing.AbstractSetdoes make sense on those versions. So a collection override fortyping.AbstractSetwould apply toMutableSet, builtin sets, and frozen sets.Since
typing.AbstractSetwas deprecated in 3.9, I don't think it's important to support it on those versions - on 3.9+ we would expect the users to switch to thecollections.abcvariants.When I say I don't consider something worth supporting, I'm doing some cost/benefit analysis of the maintenance burden of all these combinations, and as you see there are a lot of them.
So in your case, your snippet (on 3.8) would need to become:
That sound good?
layday commentedon May 4, 2022
But collection overrides themselves don't support subscripting, right?
GenConverter(unstruct_collection_overrides={collections.abc.Set[int]: sorted})won't do anything. Arguably, the converter should raise for unsupported overrides. Is there a reason not to supportcollections.abc.Setacross all Python versions? Anyway, eithertyping.AbstractSetorcollections.abc.Setwould be fine :)Tinche commentedon May 4, 2022
Started working on this here: #266
The docs say this feature isn't supported on versions prior to 3.9 btw, and I remember why not - it's very gnarly to get right across all the versions :/
Tinche commentedon May 5, 2022
I've merged this to
main, can you verify the behavior is what you'd expect?layday commentedon May 5, 2022
That's done the trick, thanks :) And I'm sorry, I missed the notice about 3.9 somehow.
Tinche commentedon May 5, 2022
Cool, ok if I close this?
layday commentedon May 5, 2022
Yep, thanks.
chore(deps): Update cattrs requirement from <22.2,>=1.8 to >=1.8,<22.…