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

Update builds signature inspection for python 3.11.4 change #497

Merged
merged 2 commits into from
Jun 30, 2023
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
3 changes: 2 additions & 1 deletion docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ hydra-zen now uses the `trusted publishers <https://blog.pypi.org/posts/2023-04-
Improvements
------------
- :func:`~hydra_zen.builds` now has a transitive property that enables iterative build patterns. See :pull:`455`
- :func:`~hydra_zen.zen`'s instantiation phase has been improved so that dataclass objects and stdlib containers are returned instead of omegaconf objects. See :pull:`#448`.
- :func:`~hydra_zen.zen`'s instantiation phase has been improved so that dataclass objects and stdlib containers are returned instead of omegaconf objects. See :pull:`448`.
- :func:`~hydra_zen.zen` can now be passed `resolve_pre_call=False` to defer the resolution of interpolated fields until after `pre_call` functions are called. See :pull:`460`.

Bug Fixes
Expand All @@ -68,6 +68,7 @@ Most of these changes will not have any impact on users, based on download stati
- :func:`~hydra_zen.just` not longer returns a frozen dataclass (see :pull:`459`).
- Users that relied on patterns like `builds(builds(...))` will find that :pull:`455` has changed their behaviors. This new behavior can be disabled via `builds(..., zen_convert={'flat_target': False})`
- :func:`~hydra_zen.zen`'s instantiation behavior was changed by :pull:`448`. See that PR for instructions on restoring the old behavior.
- The signature-inspection logic of :func:`~hydra_zen.builds` has been modified to adopt and backport a fix made to :py:func:`inspect.signature` in Python 3.11.4. See :pull:`497`.

--------------------------
Documentation - 2023-03-11
Expand Down
53 changes: 27 additions & 26 deletions src/hydra_zen/structured_configs/_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,26 @@ def sanitized_field(
return _utils.field(default=value, init=init)


def _get_sig_obj(target):
if not inspect.isclass(target):
return target

# This implements the same method prioritization as
# `inspect.signature` for Python >= 3.9.1
if "__new__" in target.__dict__:
return target.__new__
if "__init__" in target.__dict__:
return target.__init__

if len(target.__mro__) > 2:
for parent in target.__mro__[1:-1]:
if "__new__" in parent.__dict__:
return target.__new__
elif "__init__" in parent.__dict__:
return target.__init__
return getattr(target, "__init__", target)


# partial=False, pop-sig=True; no *args, **kwargs, nor builds_bases
@overload
def builds(
Expand Down Expand Up @@ -1854,6 +1874,8 @@ def builds(target, populate_full_signature=False, **kw):
)
)

_sig_target = _get_sig_obj(target)

try:
# We want to rely on `inspect.signature` logic for raising
# against an uninspectable sig, before we start inspecting
Expand Down Expand Up @@ -1884,14 +1906,9 @@ def builds(target, populate_full_signature=False, **kw):
# This looks specifically for the scenario that the target
# has inherited from a parent that implements __new__ and
# the target implements only __init__.
if (
inspect.isclass(target)
and len(target.__mro__) > 2
and "__init__" in target.__dict__
and "__new__" not in target.__dict__
and any("__new__" in parent.__dict__ for parent in target.__mro__[1:-1])
):
_params = tuple(inspect.signature(target.__init__).parameters.items())

if _sig_target is not target:
_params = tuple(inspect.signature(_sig_target).parameters.items())

if (
_params and _params[0][1].kind is not _VAR_POSITIONAL
Expand Down Expand Up @@ -1940,25 +1957,9 @@ def builds(target, populate_full_signature=False, **kw):
# `get_type_hints` properly resolves forward references, whereas annotations from
# `inspect.signature` do not
try:
if inspect.isclass(target):
# This implements the same method prioritization as
# `inspect.signature` for Python >= 3.9.1
if "__new__" in target.__dict__:
_annotation_target = target.__new__
elif "__init__" in target.__dict__:
_annotation_target = target.__init__
elif len(target.__mro__) > 2 and any(
"__new__" in parent.__dict__ for parent in target.__mro__[1:-1]
):
_annotation_target = target.__new__
else:
_annotation_target = target.__init__
else:
_annotation_target = target

type_hints = get_type_hints(_annotation_target)
type_hints = get_type_hints(_sig_target)

del _annotation_target
del _sig_target
# We don't need to pop self/class because we only make on-demand
# requests from `type_hints`

Expand Down
12 changes: 6 additions & 6 deletions tests/test_signature_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,32 +510,32 @@ def test_populate_annotated_enum_regression():
class A:
# Manually verified using `inspect.signature` after
# the fix of https://bugs.python.org/issue40897
py_310_sig = (("a", int),)
expected_sig = (("a", int),)

def __new__(cls, a: int):
return object.__new__(cls)


class B(A):
py_310_sig = (("b", float),)
expected_sig = (("b", float),)

def __init__(self, b: float):
pass


class C(A):
py_310_sig = (("c", str),)
expected_sig = (("c", str),)

def __new__(cls, c: str):
return object.__new__(cls)


class D(A):
py_310_sig = (("a", int),)
expected_sig = (("a", int),)


class E(B):
py_310_sig = (("a", int),)
expected_sig = (("b", float),)


@pytest.mark.parametrize("Obj", [A, B, C, D, E])
Expand All @@ -548,7 +548,7 @@ def test_parse_sig_with_new_vs_init(Obj):
(p.name, p.annotation) for p in inspect.signature(Conf).parameters.values()
)

assert sig_via_builds == Obj.py_310_sig
assert sig_via_builds == Obj.expected_sig


def test_Counter():
Expand Down