Skip to content

[BUG] Autoreload: disabled positonal arguments cannot change their default #14145

Open

Description

@pestrela

Issue

Autoreload doesnt update the default values for "disabled positonal arguments" because it misses kwdefaults on list func_attrs.
https://github.com/ipython/ipython/blob/main/IPython/extensions/autoreload.py#L295

Background

In python3 you can disable positonal arguments by using a "naked star" in the signature.
this is called reqkwargs (required keyword arguments).
see slide 21 of https://www.asmeurer.com/python3-presentation/python3-presentation.pdf

Demo

$ ipython --profile=default
%load_ext autoreload
%autoreload 3

with open('tmp.py', 'w') as f:
    f.write('def f(*, a=1): return a')
import tmp

print(tmp.f())
with open('tmp.py', 'w') as f:
    f.write('def f(*, a=2): return a')
print(tmp.f())

Actual Output

Python 3.6.9 (default, Nov 25 2022, 14:10:45)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.

1
1

Expected Output

1
2

Solution

The fix is simply to add the "kwdefaults" attribute to func_attrs dict in
$VENV/lib/python3.9/site-packages/IPython/extensions/autoreload.py , around line 282
https://github.com/ipython/ipython/blob/main/IPython/extensions/autoreload.py#L295

func_attrs = [
    "__code__",
    "__defaults__",       #  <<<<<  regular keyword arguments go here
    "__doc__",
    "__closure__",
    "__globals__",
    "__dict__",
    "__kwdefaults__",    #   <<<<<<<<<<<  just add this line to "IPython/extensions/autoreload.py#L282"
]

Activity

  1. Carreau commented on Aug 28, 2023

    @Carreau
    Member

    Would you be willing to send a PR with the fix, and maybe a test to make sure there is no regressions ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions