-
Notifications
You must be signed in to change notification settings - Fork 57
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 type annotation to read-only fields #2930
Comments
Hello @dimbleby I believe the models you're referencing are from our older form of generation from swagger. I don't have any solutions off the top of my head, @kristapratico do you have any ideas for how mypy could be happier here? The new models we generate are from definitions in TypeSpec, and here is an example, I believe this would conform to what you're expecting. What service are you working on, and are they moving to TypeSpec? |
Are the solutions I proposed no good? I am reporting this mostly as a user of generated APIs rather than as a person who generates them - though I do wear both hats. So far as I know I have not yet encountered any that were generated from typespec. None of the services that I work on or near have migrated to use typespec. If typespec-based generation is better then that's super and I'm all for it! But I expect that raw swagger will be around for quite some time, so if we can improve the generation from swagger I would be all for that too. |
I think doing self.read_only_field: Optional[str] = None is an improvement. It still requires the user to do None checks, but at least type checkers will now understand it must be a string if it's not None. Even better if it's not Optional and we can set it to the value of the string literal. @dimbleby can you share what services you work on / use? Best case we switch over to generation from typespec and get to use the new models. |
the immediate trigger for raising this issue was having to suppress warnings while working with But I've also recently encountered similar with the name: str = identity.name # type: ignore[assignment] and these all felt familiar when I got there, none of these are the first time I've seen this. I'm certain that we could find many many examples of mypy-thinks-readonly-fields-are-None if we looked. |
Read-only fields are currently generated in models like this
which is confusing in a couple of ways:
None
is not a stringself.read_only_field = None
None
and not astr
Would it be possible to initialize the field as
(or whatever type is appropriate)
or maybe - if the API does not declare the type nullable - just
not initializing it at all but only declaring the type?
The text was updated successfully, but these errors were encountered: