Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use of TypeVar on generic subclasses #842

merged 6 commits into from Sep 30, 2019


Copy link

zpencerq commented Sep 29, 2019

This change was originally introduced to support generic functions on generic types defined for pydantic.

Notably, passing a TypeVar on a GenericModel subclass raises a TypeError (presumably?) meant for assisting users in defining their generic model classes.


import typing
from pydantic.generics import GenericModel

T = typing.TypeVar('T')
R = typing.TypeVar('R')

class Model(GenericModel, typing.Generic[T]):
  items: typing.List[T]

# Next line raises: 
# TypeError: Type parameters should be placed on typing.Generic, not GenericModel
def convert(model: Model[T], mapper: typing.Callable[[T], R]) -> Model[R]: 
  return Model[R](items=list([mapper(item) for item in model.items]))

Change Summary

  • Allow use of TypeVar on GenericModel subclasses.


  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.rst file added describing change
    (see changes/ for details)
@zpencerq zpencerq force-pushed the zpencerq:generic_subclasses branch from 1da795d to 4031b90 Sep 29, 2019

This comment has been minimized.

Copy link

codecov bot commented Sep 29, 2019

Codecov Report

Merging #842 into master will not change coverage.
The diff coverage is n/a.

@@          Coverage Diff          @@
##           master   #842   +/-   ##
  Coverage     100%   100%           
  Files          17     17           
  Lines        3024   3024           
  Branches      582    582           
  Hits         3024   3024

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6320d8c...ea31f58. Read the comment docs.

pydantic/ Outdated Show resolved Hide resolved

This comment has been minimized.

Copy link

dmontagu commented Sep 29, 2019

@zpencerq Yes, I noticed recently that this also prevents you from using bounds to restrict the TypeVars in subclasses. Thanks for contributing this.

You should be able to fix the formatting issues currently causing tests to fail by running make format from the pydantic root folder.

You can also run a plain make to check that the CI checks will pass.

(More detail here:

zpencerq added a commit to zpencerq/pydantic that referenced this pull request Sep 30, 2019

This comment has been minimized.

Copy link
Contributor Author

zpencerq commented Sep 30, 2019

Should I open this against (or change base to) v0.32.x as well?


This comment has been minimized.

Copy link

dmontagu commented Sep 30, 2019

@zpencerq I think just against master is fine — I think @samuelcolvin is hoping to release a beta this week.

@samuelcolvin samuelcolvin merged commit 2539835 into samuelcolvin:master Sep 30, 2019
10 checks passed
10 checks passed
Header rules No header rules processed
Pages changed All files already uploaded
Redirect rules No redirect rules processed
Mixed content No mixed content detected
codecov/project 100% remains the same compared to 6320d8c
continuous-integration/travis-ci/pr The Travis CI build passed
deploy/netlify Deploy preview ready!
samuelcolvin.pydantic Build #20190930.15 succeeded
samuelcolvin.pydantic (Job Python36) Job Python36 succeeded
samuelcolvin.pydantic (Job Python37) Job Python37 succeeded

This comment has been minimized.

Copy link

samuelcolvin commented Sep 30, 2019

thanks so much.

@zpencerq zpencerq deleted the zpencerq:generic_subclasses branch Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.