operator icon indicating copy to clipboard operation
operator copied to clipboard

_JujuContext.version default factory has no effect in practice.

Open dimaqq opened this issue 10 months ago • 1 comments

Our current code:

@dataclasses.dataclass(frozen=True)
class _JujuContext:
    ...
    version: JujuVersion = dataclasses.field(default_factory=lambda: JujuVersion('0.0.0'))

However, the class is never instantiated directly, instead it's create from the environment, like so:

    @classmethod
    def from_dict(cls, env: Mapping[str, Any]) -> _JujuContext:
        return _JujuContext(
            ...
            version=JujuVersion(env['JUJU_VERSION']),
            ...
        )

If JUJU_VERSION is missing in the environment, the factory method dies with KeyError. And if it is present, the value is used.

The default factory is never used.

dimaqq avatar Jun 08 '25 02:06 dimaqq

I figure this is a valid Sunday item.

dimaqq avatar Jun 08 '25 02:06 dimaqq

If JUJU_VERSION is missing in the environment, the factory method dies with KeyError.

This isn't true after the changes in #1840.

The default factory is never used.

None of the default values are currently used, because we're only using from_dict to create these objects. However, we are considering making the class public in the future, and I don't see any value in removing all the default values now just to add them back at that time. Even if we never do that, do we really gain anything?

I suggest we close this PR and if/when we make _JujuContext public look at the default values at that time.

tonyandrewmeyer avatar Jul 04 '25 04:07 tonyandrewmeyer

Unassigning myself from this as this should be resolved by the API changes in the spec to make _JujuContext public. I'm leaving the issue open, but no objections if it gets closed.

james-garner-canonical avatar Jul 11 '25 03:07 james-garner-canonical

Closing as I believe #1996 makes this out-of-date.

tonyandrewmeyer avatar Aug 27 '25 07:08 tonyandrewmeyer