-
-
Notifications
You must be signed in to change notification settings - Fork 84
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(types): remove definition of StrEnum as Enum #881
fix(types): remove definition of StrEnum as Enum #881
Conversation
Thanks, can you add type checking tests for this? https://prisma-client-py.readthedocs.io/en/stable/contributing/contributing/#type-tests You'll have to create a new file at |
I think we still need the e.g. if TYPE_CHECKING:
if sys.version < (3, 11):
from strenum import StrEnum as StrEnum
else:
from enum import StrEnum as StrEnum
else:
# ... original try catch |
I am looking into this. Are there specific tests you have in mind that are not covered by other locations that utilize enums? i.e lists.py has several locations where it utilizes enums. Do we want more involved tests than are covered here? Or some similar tests explicitly made for enums? |
Is there a benefit to maintaining the original try-except if the if-else statement handles checking python versions and importing the StrEnum? |
That's a good point, I have a personal bias towards keeping the try-except but not for any logical reasons so feel free to just use the if statement 😅 |
I was thinking similar tests explicitly for enums, for example, the most important one would be a test case for the example code you shared before that broke when we were using |
Looks like mypy is reporting errors from the |
The I think this'll be a problem for me as well locally as I use 3.11 when developing, so I think the solution would be to add |
@fisher60 the lint error is because you need to |
hmm looks like mypy really doesn't like our usage of if TYPE_CHECKING:
if sys.version_info >= (3, 11):
from enum import StrEnum as StrEnum
else:
class StrEnum(str, Enum):
...
else:
if sys.version_info >= (3, 11):
from enum import StrEnum as StrEnum
else:
from strenum import StrEnum as StrEnum |
This addresses an issue with typing where string enums generated by prisma are not compatible with str typing. This specifically fixes an issue where Pyright checks would fail since prisma StrEnum has type Enum, which is not valid as a str.
As per the discussion in the related pull request, I have added back the TYPE_CHECKING check to prevent issues with linters for the try-except import. I have left the try-except in as-per the discussion, but I am unsure of the benefit to having both the check and the try-except.
We are now explicitly checking the python version being used when deciding which StrEnum to import, so the try-except is no longer needed. Since the try-except was removed, the `if TYPE_CHECKING` check does not do much here, so I removed it as well.
…t schema.prisma, add dev dependency for strenum package
This was erroneously removed and should now be correct again.
attempting to import strenum was causing mypy errors, so I have refactored the StrEnum to either be typed as StrEnum from enum, or as (str, Enum). This seems to be compatible with both pyright and mypy for typing.
ece35bf
to
212f179
Compare
this should no longer be necessary due to previous changes
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.
Thank you!!
This addresses an issue with typing where string enums generated by prisma are not compatible with str typing. This specifically fixes an issue where Pyright checks would fail since prisma StrEnum has type Enum, which is not valid as a str.
Change Summary
Please summarise the changes this pull request is making here.
Checklist
Agreement
By submitting this pull request, I confirm that you can use, modify, copy and redistribute this contribution, under the terms of your choice.