Skip to content

GenConverter unstructures tuple to list if in union type.  #214

Closed

Description

@aha79
  • cattrs version: 1.10
  • Python version: 3.9.7
  • Operating System: Windows 10

Description

GenConverter incorrectly unstructures tuples when in union (or optional). Converter does do it correctly.

Example:

from typing import Optional, Tuple
import cattr

orig = (1,'2')

c = cattr.Converter()
print( c.unstructure(orig, Tuple[int,str] ) )
# -> (1, '2')
print( c.unstructure(orig, Optional[Tuple[int,str]] ) )
# -> (1, '2')

c = cattr.GenConverter()
print( c.unstructure(orig, Tuple[int,str] ) )
# -> (1, '2')
print( c.unstructure(orig, Optional[Tuple[int,str]] ) )
# -> [1, '2']    <<< incorrect, no more tuple

Activity

  1. Tinche commented on Jan 26, 2022

    @Tinche
    Member

    I don't think it'd be too difficult to fix, but why is it a problem?

    The code essentially boils down to:

    >>> from cattrs import GenConverter
    >>> c = GenConverter()
    >>> c.unstructure((1,2))  # equivalent to: unstructure((1,2), tuple)
    [1, 2]
    

    Lists are better as the default here because:

    • they are actually faster, [x for x in y] is faster than tuple(x for x in y)
    • more serialization libraries support them (actually, all of them), while not all support tuples
  2. Tinche commented on Jan 26, 2022

    @Tinche
    Member

    As a small tip, if you really need them to stay tuples:

    >>> c = GenConverter(unstruct_collection_overrides={tuple: tuple})
    >>> c.unstructure((1,2))
    (1, 2)
    >>> c.unstructure((1,2), Optional[Tuple[int, int]])
    (1, 2)
    
  3. aha79 commented on Jan 27, 2022

    @aha79
    Author

    I see. I am fine with tuples always being structured into lists. And now that I think about it, I see your point with favoring list instead of tuple.

    Perhaps the real issue is that the behavior is inconsistent.

    When Tuple is unstructured directly we get tuple, but Tuple inside Optional we get list. And it is not the nesting that causes this (Tuple inside of List generates tuple), it is only inside Union (or Optional).

    I dont see any reason for this behavior. Naturally I would expect that if I put Optional around something I get the same unstructuring behavior. The difference would just be that None is also allowed. But that does not make much of a difference for unstructuring as it does not validate either.

  4. Tinche commented on Jan 27, 2022

    @Tinche
    Member

    Hm I think you're right in that our Optional handling is inconsistent. I can add a test for this.

  5. bibajz commented on Feb 10, 2022

    @bibajz
    Contributor

    Hi, this issue is also fixed by #227 - both cases are now unstructured to list

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