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

add type annotation to read-only fields #2930

Open
dimbleby opened this issue Nov 21, 2024 · 4 comments
Open

add type annotation to read-only fields #2930

dimbleby opened this issue Nov 21, 2024 · 4 comments

Comments

@dimbleby
Copy link
Member

Read-only fields are currently generated in models like this

"""Properties of the resource.

:ivar read_only_field: Some read-only string
:vartype read_only_field: str
"""

_validation = {
     "read_only_field": {"readonly": True},
]
_attribute_map = {
    "read_only_field": {"key": "readOnlyField", "type": "str"},
}

self.read_only_field = None

which is confusing in a couple of ways:

  • None is not a string
  • the only part of this that mypy understands is self.read_only_field = None
    • so every time I try to check the value of that field I have to deal with mypy insisting that I have a None and not a str

Would it be possible to initialize the field as

self.read_only_field: Optional[str] = None

(or whatever type is appropriate)

or maybe - if the API does not declare the type nullable - just

self.read_only_field: str

not initializing it at all but only declaring the type?

@iscai-msft
Copy link
Contributor

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?

@dimbleby
Copy link
Member Author

I don't have any solutions off the top of my head

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.

@kristapratico
Copy link
Member

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.

@dimbleby
Copy link
Member Author

@dimbleby can you share what services you work on / use?

the immediate trigger for raising this issue was having to suppress warnings while working with Microsoft.Kubernetes eg here

But I've also recently encountered similar with the CustomLocation from Microsoft.ExtendedLocation and with the Identity from Microsoft.ManagedIdentity, in both cases finding myself writing code like

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.

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

No branches or pull requests

3 participants