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

Add import compat layer to __init__ #24

Merged
merged 11 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ coverage:
typing:
flags:
- MyPy
target: 70% # 100%
target: 68.5% # 100%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdraco why did you lower this? The currently reported number is 87.50% per https://app.codecov.io/gh/aio-libs/propcache?flags%5B0%5D=MyPy. Honestly, I'd prefer MyPy coverage to be fixed rather than discarded. Looking into https://app.codecov.io/gh/aio-libs/propcache/blob/master/tests%2Ftest_init.py?flags%5B0%5D=MyPy, most of it is not that hard to address.

Copy link
Member Author

@bdraco bdraco Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept getting different results on each run so I under the impression it was an order of delivery problem to Codecov.

Do you have a suggestion on how to type propcache_module?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess ModuleType will work here

def imported_module(self) -> ModuleType:

Copy link
Member Author

@bdraco bdraco Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doors closing, switching to mobile, will do more if wifi works in flight

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#33

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#34

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why its missing on these lines?

Screenshot 2024-10-06 at 6 16 32 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdraco try using the from .api import cached_property as cached_property syntax in __init__.py. I've been trying to figure out similar things recently, and the experience needs improving: https://discuss.python.org/t/is-there-any-tool-that-can-report-type-coverage-of-a-project/34962/4.
It might be a rule we disable, but it still influences the coverage precision metric. So it wouldn't show up in the console output, but type coverage would be incomplete. I also saw mentions of when something in the type resolves to Any, that would make the line imprecise/uncovered. And MyPy does not have a # pragma: no cover to make it go away.


...
1 change: 0 additions & 1 deletion CHANGES/19.breaking.rst

This file was deleted.

3 changes: 3 additions & 0 deletions CHANGES/19.packaging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Moved :func:`propcache.api.under_cached_property` and :func:`propcache.api.cached_property` to ``propcache.api`` -- by :user:`bdraco`.

Both decorators remain importable from the top-level package, however importing from `propcache.api` is now the recommended way to use them.
1 change: 1 addition & 0 deletions CHANGES/24.packaging.rst
28 changes: 26 additions & 2 deletions src/propcache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
"""propcache: An accelerated property cache for Python classes."""

__version__ = "1.0.0.dev0"
from typing import TYPE_CHECKING, List

__version__ = "0.2.0.dev0"

# Imports have moved to `propcache.api` in 1.0.0+.
__all__ = ()
# This module is now a facade for the API.
if TYPE_CHECKING:
from .api import cached_property, under_cached_property
bdraco marked this conversation as resolved.
Show resolved Hide resolved

__all__ = ("cached_property", "under_cached_property")
bdraco marked this conversation as resolved.
Show resolved Hide resolved


def _import_facade(attr: str) -> object:
"""Import the public API from the `api` module."""
if attr in __all__:
from . import api # pylint: disable=import-outside-toplevel

return getattr(api, attr)
raise AttributeError(f"module 'propcache' has no attribute '{attr}'")
bdraco marked this conversation as resolved.
Show resolved Hide resolved


def _dir_facade() -> List[str]:
"""Include the public API in the module's dir() output."""
return [*__all__, *globals().keys()]


__getattr__ = _import_facade
__dir__ = _dir_facade
45 changes: 45 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test imports can happen from top-level."""

import pytest

import propcache
Dismissed Show dismissed Hide dismissed
from propcache import _helpers


def test_api_at_top_level():
"""Verify the public API is accessible at top-level."""
assert propcache.cached_property is not None
assert propcache.under_cached_property is not None
assert propcache.cached_property is _helpers.cached_property
assert propcache.under_cached_property is _helpers.under_cached_property


@pytest.mark.parametrize(
"prop_name",
("cached_property", "under_cached_property"),
)
@pytest.mark.parametrize(
"api_list", (dir(propcache), propcache.__all__), ids=("dir", "__all__")
)
def test_public_api_is_in_inspectable_object_lists(prop_name, api_list):
"""Verify the public API is discoverable programmatically.

This checks for presence of known public decorators module's
``__all__`` and ``dir()``.
"""
assert prop_name in api_list


def test_importing_invalid_attr_raises():
"""Verify importing an invalid attribute raises an AttributeError."""
match = r"^module 'propcache' has no attribute 'invalid_attr'$"
with pytest.raises(AttributeError, match=match):
propcache.invalid_attr
webknjaz marked this conversation as resolved.
Show resolved Hide resolved


def test_import_error_invalid_attr():
"""Verify importing an invalid attribute raises an ImportError."""
# No match here because the error is raised by the import system
# and may vary between Python versions.
with pytest.raises(ImportError):
from propcache import invalid_attr # noqa: F401
bdraco marked this conversation as resolved.
Show resolved Hide resolved
Loading