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

Compatibility issues between base and C-accelerated pyyaml types #7768

Closed
masklinn opened this issue May 2, 2022 · 3 comments
Closed

Compatibility issues between base and C-accelerated pyyaml types #7768

masklinn opened this issue May 2, 2022 · 3 comments

Comments

@masklinn
Copy link

masklinn commented May 2, 2022

pyyaml does not implicitly swap out the C-accelerated types for the base ones because there are observable differences between the two (yaml/pyyaml#436).

However for packages only expected relatively simple inputs, a common pattern is to try and use the libYAML types if available and fallback:

try:
    from yaml import CLoader as Loader
except ImportError:
    from yaml import Loader

However running this through mypy yields:

error: Incompatible import of "Loader" (imported name has type "Type[Loader]", local name has type "Type[CLoader]")

While this is technically correct, it's not exactly convenient. Is there a local workaround aside from type: ignore, or a way to fix this in the stubs?

Tested with:

  • CPython 10.4
  • mypy 0.950
  • types_PyYAML 6.0.7
@JelleZijlstra
Copy link
Member

I don't think we can fix this in typeshed. I believe there are already some mypy issues about this pattern.

@AlexWaygood
Copy link
Member

AlexWaygood commented May 2, 2022

I don't think we can fix this in typeshed. I believe there are already some mypy issues about this pattern.

@Akuli
Copy link
Collaborator

Akuli commented May 2, 2022

Is there a local workaround aside from type: ignore

Yes, but it's much uglier than # type: ignore:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from yaml import Loader
else:
    try:
        from yaml import CLoader as Loader
    except ImportError:
        from yaml import Loader

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

No branches or pull requests

4 participants