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

Use type vars for resource registration to return the same type #59

Merged
merged 2 commits into from
Feb 19, 2024

Conversation

tim-hutchinson
Copy link
Contributor

@tim-hutchinson tim-hutchinson commented Feb 16, 2024

ResourceRegistry.register() currently indicates that it returns one a type that is a subclass of res.NamespacedResource or res.GlobalResource. However, that leads type checkers to only be able to check against those definitions, losing all the type information defined on the subclass.

Example

from dataclasses import dataclass
from lightkube.codecs import resource_registry
from lightkube.core.resource import NamespacedResourceG

@dataclass
class ExampleClass(NamespacedResourceG):
	foo: str = 'bar'

# Currently, EC will have the type Type[Union[NamespacedResource, GlobalResource]], rather than ExampleClass
EC = resource_registry.register(ExampleClass)

ec = EC()
# Will show a type error for foo not being a defined attribute on either NamespacedResource or GlobalResource
ec.foo

This changes the register signature to use a TypeVar instead, to indicate that the type returned is the same type as the input

@gtsystem
Copy link
Owner

Hi, looks good. Can you also increase the patch version in setup.py?

@tim-hutchinson
Copy link
Contributor Author

@gtsystem - Done

@gtsystem gtsystem merged commit 90fd41f into gtsystem:master Feb 19, 2024
8 checks passed
@gtsystem
Copy link
Owner

Fix available in v0.15.2

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

Successfully merging this pull request may close these issues.

2 participants