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.

"from" keyword in data to validate raises an Syntax error

See original GitHub issue

Hello,

I’m running into a problem when validating the following data with pydantic:

{ "validity_dates": { "from": "2014-12-31", "to": "2015-12-31" } } It’s the “from” key in the data that is problematic:

class ValidityDates(pydantic.BaseModel): from: str to: str

This code raises a SyntaxError: invalid syntax. How can I map a supported class attribute like “from_” to the dict key “from” when:

  1. validating the data
  2. serialising the model

Thanks fo your help

Issue Analytics

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

github_iconTop GitHub Comments

16reactions
samuelcolvincommented, Apr 9, 2018
from datetime import date
from pydantic import BaseModel


class Foobar(BaseModel):
    from_: date

    class Config:
        fields = {
            'from_': 'from'
        }


foobar = Foobar(**{'from': '2014-12-31'})
print(foobar)
print(repr(foobar.from_))
7reactions
samuelcolvincommented, Apr 9, 2018

No problem, surely

def dict(self, *args, **kwargs):
    d = super().dict(*args, **kwargs)
    d['from'] = d.pop('from_')
    return d

Would be simpler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"from" keyword in data to validate raises an Syntax error #153
Hello, I'm running into a problem when validating the following data with pydantic: ... "from" keyword in data to validate raises an Syntax...
Read more >
Invalid Syntax in Python: Common Reasons for SyntaxError
When the interpreter encounters invalid syntax in Python code, it will raise a SyntaxError exception and provide a traceback with some helpful information...
Read more >
Errors and exceptions — Object-Oriented Programming in ...
Common Python syntax errors include: leaving out a keyword; putting a keyword in the wrong place; leaving out a symbol, such as a...
Read more >
Python code works, but eclipse shows error - Syntax error ...
At the line print(r, end = '') , eclipse reports a syntax error - Syntax error while detecting tuple . However, the program...
Read more >
SQL Error: "Syntax error at or near:" - Looker Community
Overview. This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: Using a database-specific ...
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