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

Confusing Error/Potential but with Python 3.6 since 5.10.0 #271

Closed
MyreMylar opened this issue Oct 15, 2022 · 2 comments
Closed

Confusing Error/Potential but with Python 3.6 since 5.10.0 #271

MyreMylar opened this issue Oct 15, 2022 · 2 comments
Labels
invalid This doesn't seem right

Comments

@MyreMylar
Copy link

Hello,

I've been using importlib since the days of the old interface so I preface by saying I may be doing something wrong. In the last week my unit tests started failing with a compile error in this chunk of code here:

/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/site-packages/importlib_resources-5.10.0-py3.6.egg/importlib_resources/_common.py:46: in wrapper
    return func(anchor)
/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/site-packages/importlib_resources-5.10.0-py3.6.egg/importlib_resources/_common.py:56: in files
    return from_package(resolve(anchor))
/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/site-packages/importlib_resources-5.10.0-py3.6.egg/importlib_resources/_common.py:[112](https://github.com/MyreMylar/pygame_gui/actions/runs/3255020934/jobs/5343897993#step:5:113): in from_package
    spec = wrap_spec(package)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

package = 'pygame_gui.data'

    def wrap_spec(package):
        """
        Construct a package spec with traversable compatibility
        on the spec/loader/reader.
    
        Supersedes _adapters.wrap_spec to use TraversableResourcesLoader
        from above for older Python compatibility (<3.10).
        """
        from . import _adapters
    
>       return _adapters.SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
E       AttributeError: 'str' object has no attribute '__spec__'

it seems clear that it is not expecting a string at this point, but the same code on my side is compiling and testing happily on 3.7. I also notice that a new version of importlib_resources came out in the past week so I'm assuming this is related to my new failure.

Here is where I'm using it in my code:

self.loaded_font = pygame.freetype.Font(
                    io.BytesIO((resources.files(self.location.package) /
                                self.location.resource).read_bytes()),
                    self.size, resolution=72)

The intention here being to load a font resource from a package, read it as bytes into a BytesIO object and then use that to create a pygame freetype font object, location.package and location.resource are both strings.

Am I doing something wrong here or is this a bug?

@jaraco
Copy link
Member

jaraco commented Oct 16, 2022

It sure does seem like a bug, except for one key factor:

Since 5.5.0, importlib_resources requires Python 3.7 or later. This requirement is declared in metadata, which ideally should prevent the installation of these newer versions on older Pythons.

Since you're not able to replicate the issue on Python 3.7 or on older importlib_resources, I don't believe you have a supported use-case.

Still, that doesn't explain why you're encountering the error.

Oh, I bet I know what the issue is. importlib_resources 5.10 uses single dispatch to resolve a package "anchor" to a module, and it uses the type annotations to decide, capability that was added in Python 3.7. Because type annotations aren't honored on Python 3.6, it means that importlib_resources._common.resolve will only resolve a ModuleType parameter.

I'm slightly surprised that resolve(<str>) doesn't create a type error when it's cast to a ModuleType. But indeed, that seems to be how cast works:

>>> cast(types.ModuleType, 'foo')
'foo'

I see that behavior is documented and expected for performance reasons and only affects type checking.

Let me know if you think there's something else that importlib_resources should be doing here.

@jaraco jaraco closed this as completed Oct 16, 2022
@jaraco jaraco added the invalid This doesn't seem right label Oct 16, 2022
@MyreMylar
Copy link
Author

Thanks for the explanation, I was able to pin importlib_resources to an older version for python 3.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants