Skip to content

typing.Set (built-in set type) is aliased to collections.abc.Set (abstract set) #264

Closed

Description

@layday
  • cattrs version: 22.1.0
  • Python version: 3.8
  • Operating System: macOS

Description

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

from collections.abc import Set

converter = GenConverter(unstruct_collection_overrides={Set: sorted})
converter.unstructure(frozenset(()))  # Not converted to a list on Python 3.8

Activity

  1. Tinche commented on May 4, 2022

    @Tinche
    Member

    Ooph, let's see, it's a little confusing.

    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(()))
    

    That sound good?

  2. layday commented on May 4, 2022

    @layday
    ContributorAuthor

    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 :)

  3. Tinche commented on May 4, 2022

    @Tinche
    Member

    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 :/

  4. Tinche commented on May 5, 2022

    @Tinche
    Member

    I've merged this to main, can you verify the behavior is what you'd expect?

  5. layday commented on May 5, 2022

    @layday
    ContributorAuthor

    That's done the trick, thanks :) And I'm sorry, I missed the notice about 3.9 somehow.

  6. Tinche commented on May 5, 2022

    @Tinche
    Member

    Cool, ok if I close this?

  7. layday commented on May 5, 2022

    @layday
    ContributorAuthor

    Yep, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions