question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple attribute fields for the class attribute

See original GitHub issue

Like it shown in the example:

class UserSchema(Schema):
    name = fields.String()
    email_addr = fields.String(attribute="email")
    date_created = fields.DateTime(attribute="created_at")

user = User('Keith', email='keith@stones.com')
ser = UserSchema()
result = ser.dump(user)
pprint(result)
# {'name': 'Keith',
#  'email_addr': 'keith@stones.com',
#  'date_created': '2014-08-17T14:58:57.600623+00:00'}

It would be great to be able to specify multiple attributes like it done in golang tags. https://stackoverflow.com/questions/18635671/how-to-define-multiple-name-tags-in-a-struct

something like this:

class UserSchema(Schema):
    name = fields.String()
    email_addr = fields.String(attributes=("email", "e-mail"))
    date_created = fields.DateTime(attribute="created_at")

user = User('Keith', email='keith@stones.com')
ser = UserSchema()
result = ser.dump(user)
pprint(result)
# {'name': 'Keith',
#  'email_addr': 'keith@stones.com',
#  'date_created': '2014-08-17T14:58:57.600623+00:00'}

user = User('Keith', e-mail='keith@stones.com')
ser = UserSchema()
result = ser.dump(user)
pprint(result)
# {'name': 'Keith',
#  'email_addr': 'keith@stones.com',
#  'date_created': '2014-08-17T14:58:57.600623+00:00'}

Marshmallow already extensively used in testing, but this addition will open a new horizon for testing (especially in comparators related area)

If it sounds reasonable I could dig into it.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, Jun 4, 2019

OK, but what would be the logic from the Field point of view? Pick the first attribute of the list that holds a value?

The feature you’re asking for would have an undefined behaviour when dumping if both attributes are in the object.

You should use different schemas for this use case.

0reactions
LaserPhasercommented, Jun 5, 2019

In testing, you usually have to map the data from different sources like databases/API. This proposal is like some kind of such mapper.

The feature you’re asking for would have an undefined behaviour when dumping if both attributes are in the object.

Actually, we can just throw a custom exception.

You should use different schemas for this use case.

I’ve got your point if it will break marshmallow ideology let’s just close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To: Add the same field to multiple feature classes ...
The instructions provided describe how to add the same field to multiple feature classes simultaneously.
Read more >
Update Multiple Fields with a Single Attribute Rule - YouTube
Learn about a new capability with ArcGIS Pro 2.7 and Enterprise 10.9 that enables you to update multiple fields with a single attribute...
Read more >
Applying the same attribute values to multiple features in a layer
Applying the same attribute values to multiple features in a layer · Click the Edit tool · Click the Attributes button · Click...
Read more >
Can you apply a attribute to multiple fields in C#? - Stack ...
Is it possible in C# to apply a single attribute to multiple fields at once? public class MyClass { [SomeAttribute] public int m_nVar1;...
Read more >
HTML multiple Attribute - W3Schools
The multiple attribute is a boolean attribute. When present, it specifies that the user is allowed to enter/select more than one value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found