-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add conlist type #583
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
Add conlist type #583
Conversation
5a43ffc to
1f1be1e
Compare
Codecov Report
@@ Coverage Diff @@
## master #583 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2542 2582 +40
Branches 504 510 +6
=====================================
+ Hits 2542 2582 +40 |
|
I have no clue why all the tests are failing and why the coverage is reduced. I cannot really reproduce it locally except for the Cython issue... It seems Cython doesn't like the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good, thank you. Nothing major to fix, just some small things.
81296a9 to
dc64b32
Compare
|
Hmm one test is still failing on 3.6.. I don't have access to 3.6 atm. So it is a bit hard to debug that one. I am going to leave this open until I have time for this or until someone else finishes this... |
|
If you fix the current test that's failing on all versions, I'll fix the 3.6 test as I have it running locally. |
Thanks. I fixed the test.. |
|
this is still failing on lots of stuff, I guess related to how cython manages imports. |
|
Hmm fixing the Cython issue seems to have fixed 3.6... |
| class ConstrainedList(List[T]): | ||
| __origin__ = list # Needed for pydantic to detect that this is a list | ||
| # This types superclass should be List[T], but cython chokes on that... | ||
| class ConstrainedList(list): # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is a way to put something into a conditional block (possibly even with if TYPE_CHECKING) to get this to behave with Cython, but so that we don't have to give up the generic parameter for mypy type-checking purposes. Or maybe it can still just be type-hinted as List[T]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like
if TYPE_CHECKING:
class ConstrainedListSuperType(List[T]):
pass
else:
class ConstrainedListSuperType(list):
pass
class ConstrainedList(ConstrainedListSuperType):
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to implement this but I think I can only put the whole class under a conditional. The above does not work because for the typing to be correct you need: class ConstrainedList(ConstrainedListSuperType[T]): which gives the same issue...
This might be related to cython/cython#2753
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can put the whole block in if TYPE_CHECKING: but that duplicates quite a lot of code. So I would rather do it this way for now...
| class ConstrainedList(List[T]): | ||
| __origin__ = list # Needed for pydantic to detect that this is a list | ||
| # This types superclass should be List[T], but cython chokes on that... | ||
| class ConstrainedList(list): # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like
if TYPE_CHECKING:
class ConstrainedListSuperType(List[T]):
pass
else:
class ConstrainedListSuperType(list):
pass
class ConstrainedList(ConstrainedListSuperType):
...Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
|
awesome. Thanks so much. |
Change Summary
Added a conlist type, which allows you to restrict the number of items in a list
Related issue number
#161 Is related to this
Checklist
HISTORY.rsthas been updated#<number>@<whomever>