juju / python-libjuju

Python library for the Juju API
Apache License 2.0
59 stars 99 forks source link

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

Open dimaqq opened 1 week ago

dimaqq commented 1 week ago

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...

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 commented 1 week ago

Original commit 9c204c0a