Skip to content

Resource.__iter__() produces the wrong type #161

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

Closed
igalic opened this issue Oct 5, 2017 · 5 comments
Closed

Resource.__iter__() produces the wrong type #161

igalic opened this issue Oct 5, 2017 · 5 comments

Comments

@igalic
Copy link
Collaborator

igalic commented Oct 5, 2017

in implementing #131, i've tried typing this __iter__() method for Resource:

    def __iter__(
        self
    ) -> typing.Generator[Resource, None, None]:

        for child_dataset in self.dataset.children:

            name = self._get_asset_name_from_dataset(child_dataset)
            if self._filters is not None and \
               self._filters.match_key("name", name) is not True:
                # Skip all jails that do not even match the name
                continue

            # ToDo: Do not load jail if filters do not require to
            resource = self._get_resource_from_dataset(child_dataset)
            if self._filters is not None and \
               self._filters.match_resource(resource):
                yield resource  # <= 💣💣💣💣

the 💣 marks where the issue is:

iocage/lib/Resource.py:379:16: error: Incompatible types in "yield" (actual type "Generator[Resource, None, None]", expected type "Resource")

This is caused by the (supposedly abstract;) function _create_resource_instance. In the Jails.py this function returns a Jail (not a JailGenerator) — so, whom do we believe?? ;)


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@gronke
Copy link
Member

gronke commented Oct 5, 2017

JailGenerator is correct internally. We only provide a class Jail that wraps JailGenerator to ease using the lib non-async.

@igalic
Copy link
Collaborator Author

igalic commented Oct 5, 2017

the problem is yield, at this point, expects a $Resource, not a $ResourceGenerator, or it won't be able to produce a iterable that __iter__() is supposed to iterate…

@gronke
Copy link
Member

gronke commented Oct 5, 2017

Both JailGenerator and Jail are Resources. The return type of _get_resource_from_dataset depends on whether it was called on Jails or JailsGenerator. The interface https://github.com/iocage/libiocage/blob/e683b412e947cb7b078341f6313c2fe16568174f/iocage/lib/Jails.py#L63-L68 is lying when calling Jail._get_resource_from_dataset().

@igalic
Copy link
Collaborator Author

igalic commented Oct 5, 2017

related errors:

iocage/lib/Resource.py:377:15: error: Argument 1 to "match_resource" of "Terms" has incompatible type "Generator[Resource, None, None]"; expected "Resource"
iocage/lib/Resource.py:378:16: error: Incompatible types in "yield" (actual type "Generator[Resource, None, None]", expected type "Resource")
iocage/cli/list.py:115:8: error: Argument 1 to "_print_list" has incompatible type "ReleasesGenerator"; expected "Generator[JailsGenerator, None, None]"
iocage/cli/list.py:117:8: error: Argument 1 to "_print_list" has incompatible type "ReleasesGenerator"; expected "Generator[JailsGenerator, None, None]"
iocage/cli/list.py:119:8: error: Argument 1 to "_print_json" has incompatible type "ReleasesGenerator"; expected "Generator[JailsGenerator, None, None]"
iocage/cli/list.py:121:8: error: Argument 1 to "_print_table" has incompatible type "ReleasesGenerator"; expected "Generator[ListableResource, None, None]"

@gronke
Copy link
Member

gronke commented Nov 15, 2017

The PR #131 was fixed and merged.

@gronke gronke closed this as completed Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants