Skip to content

Fix type hints of parse_obj and similar methods#405

Merged
samuelcolvin merged 1 commit intopydantic:masterfrom
erosennin:generic-constructors
Feb 21, 2019
Merged

Fix type hints of parse_obj and similar methods#405
samuelcolvin merged 1 commit intopydantic:masterfrom
erosennin:generic-constructors

Conversation

@erosennin
Copy link
Copy Markdown
Contributor

@erosennin erosennin commented Feb 21, 2019

Change Summary

Methods like BaseModel.parse_obj() or BaseModel.copy() have their return type specified as BaseModel. Because of that, valid code like this:

from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str

u = User.parse_obj({
    'id': 11,
    'name': 'Acedia',
})

print(u.id, u.name)

does not type check:

example.py:12: error: "BaseModel" has no attribute "id"
example.py:12: error: "BaseModel" has no attribute "name"

This PR fixes that.

Related issue number

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

@erosennin erosennin force-pushed the generic-constructors branch from 25edbe8 to aec6968 Compare February 21, 2019 20:49
@erosennin erosennin changed the title Fix type hints of parse_obj and similar methods Fix type hints of parse_obj and similar methods Feb 21, 2019
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 21, 2019

Codecov Report

Merging #405 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #405   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          14     14           
  Lines        2068   2068           
  Branches      420    420           
=====================================
  Hits         2068   2068

Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise LGTM.

Comment thread pydantic/main.py Outdated
Comment thread HISTORY.rst Outdated
@erosennin erosennin force-pushed the generic-constructors branch from aec6968 to 7f336d4 Compare February 21, 2019 21:45
@erosennin erosennin force-pushed the generic-constructors branch from 7f336d4 to c6e5fdd Compare February 21, 2019 21:46
@erosennin
Copy link
Copy Markdown
Contributor Author

Done.

@samuelcolvin samuelcolvin merged commit e77bc00 into pydantic:master Feb 21, 2019
@samuelcolvin
Copy link
Copy Markdown
Member

awesome. Thank you very much.

I'll fix another thing and deploy in a day or two.

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
* Mostly working ChoiceKey for int keys

* Update src/validators/union.rs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* Update src/validators/union.rs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* Update src/validators/union.rs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* address comments

* address more comments

* Address more comments and fix test

* add tests for int choice keys

* fix

* add enum choices test

* update repeated tag test

* Set from_attributes to true by default for TaggedUnionValidator

* Get test passing

* fix tags_repr for ints

* refactor repeated tags test

* Use _extra.strict for literal validation of strs and ints

* support i64 location keys, tests

* change literal strict usage

---------

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants