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 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)

@codecov
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
Copy link
Contributor

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
Copy link
Contributor Author

zpencerq commented Sep 30, 2019

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

@dmontagu
Copy link
Contributor

@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 pydantic:master Sep 30, 2019
@samuelcolvin
Copy link
Member

thanks so much.

@zpencerq zpencerq deleted the generic_subclasses branch September 30, 2019 17:41
andreshndz pushed a commit to cuenca-mx/pydantic that referenced this pull request Jan 17, 2020
* Add failing generic subclass test

* Only raise type parameter failure on base GenericModel class

* Add changes to describe PR pydantic#842

* Change the class check to use is

* Fix formatting in subclass test

* correct change
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.

None yet

3 participants