-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Fix signature of Choices member creation, add assert_type
test cases, run pyright
#2162
Conversation
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.
Please, add a test for this. Since this is quite hard to understand, really.
Unfortunately not possible, this (and the previous PR) only targets pyright as mypy does not type check the arguments used when defining the enum members values (at some point -- when the spec gets updated -- it will have to). In its current state, the PR enables the following on pyright: from django.db.models import IntegerChoices, TextChoices
from django.utils.translation import gettext_lazy as _
class T(TextChoices):
A = "a" # no type checker errors
B = "b", "B label" # no type checker errors
C = "c", _("C label") # no type checker errors
D = 1 # type checker error
E = "e", 1 # type checker error
class I(IntegerChoices):
A = 1 # no type checker errors
B = "1" # no type checker errors (everything accepted by `int(...)` works)
C = 1, 2 # type checker error The extra use cases I added in my original comment are really really uncommon, in fact I even wonder if someone ever used these constructs. I just added them for documentation purposes, but I don't think it's a good idea to support them. |
Edit: I see pyright was recently added to CI, I'll see if I can add a test anyway |
Check out this script from I am not against adding something similar to our CI as well. |
Hum, so you mean writing plain Python files and make use of |
Yeap, and run the with both mypy and pyright. |
Sounds good, in the future I guess all tests might switch to using |
There are still benefits in using |
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.
Thanks! Great stuff!
As the author of a lot of the choices stuff I plan to review everything in the stubs related to it. Sorry I haven't got around to doing so yet. |
@ngnpope thank a lot, this is very appreciated! 👍 |
Before adding more assertions, I'll wait for a new release of pyright to land (in a week or so), so that this fix: microsoft/pyright#7941 gets included |
assert_type
test cases, run pyright
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 think this looks good. It's great that you got started on the assert_type
here as after this we're able to improve the test suite!
Yep, it's indeed easier to write tests (syntax highlighting, you don't depend on the phrasing of the error codes/reveal type messages). Plus it's probably way faster than the pytest plugin. I think it would be great to migrate most of the tests that don't depend on the plugin to use such assertions in the future |
I'm going to go ahead and merge this, thinking that additional changes/improvements can come in any subsequent PR. Thank you for your work done here. |
After seeing #2156 merged, I decided to go down the rabbit hole, as the fix is actually incorrect.
First of all, we should note that there is a discussion going on to specify the typing behavior of enums. I added a comment related to constructors, as this doesn't seem to be taken into account yet. My comment roughly describes the runtime behavior.
It might be better to wait for the chapter about enums to be merged, as we are currently using undefined behaviors. However, the stubs could easily be changed if necessary in the future.
Regarding the implementation, I went for an
__init__
method here, as__new__
is being used to instantiate enum members when doing a lookup likeColor(3)
.In reality, it is also possible to do the following:
TextChoices
also has a different behavior from Python 3.11 onwards, as Django conditionally subclassesenum.StrEnum
andenum.StrEnum
has a different__new__
method (but notIntEnum
for some reason). The most accurate patch would then look something like this: