Skip to content

Fix IndexError for positional-only default arguments#3

Open
theweaklink wants to merge 1 commit into
deeplime-io:onecodefrom
theweaklink:fix/posonlyargs-defaults
Open

Fix IndexError for positional-only default arguments#3
theweaklink wants to merge 1 commit into
deeplime-io:onecodefrom
theweaklink:fix/posonlyargs-defaults

Conversation

@theweaklink

Copy link
Copy Markdown

Summary

  • Fix _get_fun_defaults in PreProcessor to map default values using posonlyargs + args instead of only args, per PEP 570.
  • Add unit tests covering positional-only defaults (e.g. def eye(n_rows, n_cols=None, /)) and regular positional defaults.

This resolves IndexError: list index out of range in onecode-build / onecode-extract when PyCG statically analyzes third-party code containing positional-only parameters with defaults (common in recent scipy/numpy/sklearn sources).

Context

The PyPI package is published as onecode-pycg, sourced from this repository's onecode branch.

The crash was observed on Windows when a .venv lives inside the OneCode project directory: PyCG then treats site-packages modules as local project code and parses them. One such function is scipy's eye(n_rows, n_cols=None, /), which triggers the bug in _get_fun_defaults.

Test plan

  • python3 -m unittest discover -s pycg/tests -p "preprocessor_test.py"
  • make test (requires mock package for existing tests)
  • Run onecode-extract on a OneCode project with in-project .venv on Windows

Made with Cursor

PyCG's _get_fun_defaults only indexed node.args.args, but PEP 570
defaults apply to the combined posonlyargs + args list. Functions
such as `def eye(n_rows, n_cols=None, /)` caused IndexError during
static analysis when third-party modules were scanned.

Co-authored-by: Cursor <[email protected]>
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

Successfully merging this pull request may close these issues.

1 participant