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
merged 6 commits into from Sep 30, 2019

Conversation

@zpencerq
Copy link
Contributor

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.

Example:

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.

Checklist

  • 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/README.md for details)
@zpencerq zpencerq force-pushed the zpencerq:generic_subclasses branch from 1da795d to 4031b90 Sep 29, 2019
@codecov

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/generics.py Outdated Show resolved Hide resolved
@dmontagu

This comment has been minimized.

Copy link
Collaborator

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: https://pydantic-docs.helpmanual.io/#contributing-to-pydantic)

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

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?

@dmontagu

This comment has been minimized.

Copy link
Collaborator

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
Details
Pages changed All files already uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
codecov/project 100% remains the same compared to 6320d8c
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20190930.15 succeeded
Details
samuelcolvin.pydantic (Job Python36) Job Python36 succeeded
Details
samuelcolvin.pydantic (Job Python37) Job Python37 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

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
Projects
None yet
3 participants
You can’t perform that action at this time.