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

Type.from_json() is trying to do too much #1111

Open
dimaqq opened this issue Sep 26, 2024 · 1 comment
Open

Type.from_json() is trying to do too much #1111

dimaqq opened this issue Sep 26, 2024 · 1 comment
Labels
kind/wishlist requested feature priority/low low priority

Comments

@dimaqq
Copy link
Contributor

dimaqq commented Sep 26, 2024

Description

jRPC responses are pumped through SomeResult.from_json(data) to convert dicts to proper data classes.

the from_json() classmethod though is capable of doing so many different things...

  • it can accept the class itself, returns data unchanged
  • it can accept a plain string, which would be assumed to be JSON and then:
  • it can accept a dict, which would be converted to this data class, processing properties recursively
  • it can accept a list, which merged elements' content into a single dict, WAT?
  • it can accept "other", returning None

this means that the classmethod has a weird return type, Self|None which has cascading effects on all the users of any facade call.

Urgency

Casually reporting

Python-libjuju version

3.5.2

Juju version

any

Reproduce / Test

class ApplicationInfoResults(Type):
      _toSchema = {'results': 'results'}
      _toPy = {'results': 'results'}
      def __init__(self, results=None, **unknown_fields):
          '''
          results : typing.Sequence[~ApplicationInfoResult]
          '''
          results_ = [ApplicationInfoResult.from_json(o) for o in results or []]

          # Validate arguments against known Juju API types.
          if results_ is not None and not isinstance(results_, (bytes, str, list)):
              raise Exception("Expected results_ to be a Sequence, received: {}".format(type(results_)))

          self.results = results_
          from typing import reveal_type
W         reveal_type(self.results) # W: Type of "self.results" is "list[ApplicationInfoResult | None]"
          self.unknown_fields = unknown_fields
@dimaqq dimaqq added the kind/bug indicates a bug in the project label Sep 26, 2024
@dimaqq
Copy link
Contributor Author

dimaqq commented Sep 26, 2024

Original commit 9c204c0

@dimaqq dimaqq added priority/low low priority kind/wishlist requested feature and removed kind/bug indicates a bug in the project labels Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/wishlist requested feature priority/low low priority
Projects
None yet
Development

No branches or pull requests

1 participant