Skip to content

loader: Fix loading grains with annotations#60285

Merged
Ch3LL merged 1 commit intosaltstack:masterfrom
bdrung:fix-grain-with-annotation
Oct 11, 2021
Merged

loader: Fix loading grains with annotations#60285
Ch3LL merged 1 commit intosaltstack:masterfrom
bdrung:fix-grain-with-annotation

Conversation

@bdrung
Copy link
Contributor

@bdrung bdrung commented Jun 1, 2021

If custom grain modules use annotations, salt will fail to load them. Example grain:

from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}

salt-call grains.items will print this exception:

Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them

Python recommends to use inspect.signature instead of inspect.getfullargspec (which is only needs to maintain compatibility with the Python 2 inspect module API). Since salt only supports Python 3, drop using the wrapper function get_function_argspec and use inspect.signature directly.

@bdrung bdrung requested a review from a team as a code owner June 1, 2021 14:03
@bdrung bdrung requested review from Ch3LL and removed request for a team June 1, 2021 14:03
@bdrung bdrung force-pushed the fix-grain-with-annotation branch from cfeeab3 to ed944ab Compare June 1, 2021 14:07
bdrung added a commit to bdrung/salt that referenced this pull request Jun 1, 2021
If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Forwarded: saltstack#60285
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
Copy link
Contributor

@Ch3LL Ch3LL left a comment

Choose a reason for hiding this comment

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

Needs a changelog and a test case to ensure we don't regress the ability to use custom grains with annotations in the future.

@bdrung bdrung force-pushed the fix-grain-with-annotation branch from ed944ab to 568b63f Compare June 2, 2021 12:01
@bdrung
Copy link
Contributor Author

bdrung commented Jun 2, 2021

Added a changelog entry and test cases to prevent regressions.

Ch3LL
Ch3LL previously approved these changes Jun 2, 2021
@Ch3LL Ch3LL requested a review from s0undt3ch June 2, 2021 17:40
Copy link
Contributor

@s0undt3ch s0undt3ch left a comment

Choose a reason for hiding this comment

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

This PR has conflicts that need to be solved.

@bdrung
Copy link
Contributor Author

bdrung commented Sep 16, 2021

Rebased.

@bdrung bdrung force-pushed the fix-grain-with-annotation branch 3 times, most recently from b72d9e7 to d92ed37 Compare September 17, 2021 09:28
s0undt3ch
s0undt3ch previously approved these changes Sep 18, 2021
@Ch3LL
Copy link
Contributor

Ch3LL commented Oct 7, 2021

ping @bdrung I don't seem to have permissions to update the branch on your PR. We recently fixed the mac and debian test issues you are seeing, would you mind rebasing your branch again.

If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
@bdrung
Copy link
Contributor Author

bdrung commented Oct 8, 2021

Rebased.

@Ch3LL Ch3LL merged commit 99320f6 into saltstack:master Oct 11, 2021
@bdrung bdrung deleted the fix-grain-with-annotation branch October 12, 2021 11:05
bdrung added a commit to bdrung/salt that referenced this pull request Oct 13, 2021
If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Forwarded: saltstack#60285
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
bdrung added a commit to bdrung/salt that referenced this pull request Oct 14, 2021
If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Forwarded: saltstack#60285
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
bdrung added a commit to bdrung/salt that referenced this pull request Apr 15, 2022
If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Forwarded: saltstack#60285
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
bdrung added a commit to bdrung/salt that referenced this pull request Apr 16, 2022
If custom grain modules use annotations, salt will fail to load them.
Example grain:

```
from typing import Dict

def example_grain() -> Dict[str, str]:
    return {"example": "42"}
```

`salt-call grains.items` will print this exception:

```
Failed to load grains defined in grain file ... in function <...>, error:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 847, in grains
    parameters = salt.utils.args.get_function_argspec(funcs[key]).args
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 271, in get_function_argspec
    aspec = _getargspec(func)
  File "/usr/lib/python3/dist-packages/salt/utils/args.py", line 40, in _getargspec
    "Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
```

Python recommends to use `inspect.signature` instead of
`inspect.getfullargspec` (which is only needs to maintain compatibility with
the Python 2 `inspect` module API). Since salt only supports Python 3, drop
using the wrapper function `get_function_argspec` and use `inspect.signature`
directly.

Forwarded: saltstack#60285
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
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.

3 participants