Comments (9)
a
is a positional argument, so the decorator module is doing the right thing. It looks like you are confusing positional arguments with a default with keyword arguments. Please have a look at the Python documentation.
from decorator.
I just ran into this as well. Comments about samplefn()
below are considering it undecorated and discussing what the terms would be without decorator
being involved. It appears that at least the start of the definitions are unchanged between 2.7 and 3.8.
On the calling side of the activity they are arguments.
https://docs.python.org/2.7/glossary.html#term-argument
https://docs.python.org/3.8/glossary.html#term-argument
keyword argument: an argument preceded by an identifier (e.g. name=) in a function call
positional argument: an argument that is not a keyword argument.
It seems that a
in samplefn(1, a="askeyword")
is a keyword argument. 1
there is a positional argument. In samplefn(1, "inline")
the parameter a
will be filled by by the positional argument "inline"
.
Now on the other side, the function not the call, they are parameters.
https://docs.python.org/2.7/glossary.html#term-parameter
https://docs.python.org/3.8/glossary.html#term-parameter
positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument.
So in def samplefn(x, a="a1")
both x
and a
are positional-or-keyword parameters.
As an extra piece, yes if you def f(*, x):
then decorator
will pass x
always via kwargs.
It's not obvious to me yet whether for the default value case samplefn(1)
should put "a1"
into the positional or keyword arguments. At the moment I don't have any particular contention with decorator
's present behavior of putting it in the positional arguments. It does seem that samplefn(1, "inline")
should put "inline"
in positional args, as it does presently. Per the documentation a="askeyword"
in samplefn(1, a="askeyword")
is a keyword argument and thus ought to go into the keyword arguments, which it presently does not.
Then again, perhaps you were referring to some other section of the docs which is in conflict with the glossary?
from decorator.
I'll note that 2.6 is much less clear about all this in the glossary. It doesn't even have an entry for 'parameter' and refers to a function having arguments in its definition.
https://docs.python.org/2.6/glossary.html#term-argument
A value passed to a function or method, assigned to a named local variable in the function body. A function or method may have both positional arguments and keyword arguments in its definition. Positional and keyword arguments may be variable-length: * accepts or passes (if in the function definition or call) several positional arguments in a list, while ** does the same for keyword arguments in a dictionary.
Any expression may be used within the argument list, and the evaluated value is passed to the local variable.
from decorator.
Per the documentation
a="askeyword"
insamplefn(1, a="askeyword")
is a keyword argument and thus ought to go into the keyword arguments, which it presently does not.
I believe it is a bug in the Python documentation. The calling syntax means nothing, the distinction between positional and keyword arguments has to be done at the function definition.
from decorator.
I don't understand what the argument is for decorator.decorator
provided *args, **kwargs
to be different than those that the nominal wrapping decorator pattern results in.
https://repl.it/@altendky/VillainousTangibleObjectpool-2
import decorator
@decorator.decorator
def a(f, *args, **kwargs):
print('a', args, kwargs)
def b(f):
def wrapper(*args, **kwargs):
print('b', args, kwargs)
return wrapper
def f(a, b):
pass
af = a(f)
bf = b(f)
af(1, 2)
af(1, b=2)
af(a=1, b=2)
print()
bf(1, 2)
bf(1, b=2)
bf(a=1, b=2)
a (1, 2) {}
a (1, 2) {}
a (1, 2) {}
b (1, 2) {}
b (1,) {'b': 2}
b () {'a': 1, 'b': 2}
from decorator.
For clarity, this appears to be fixed in the latest (py3-only) release. I did not dig into what commit fixed it. Thanks.
https://replit.com/@altendky/VillainousTangibleObjectpool-3
a (1, 2) {}
a (1,) {'b': 2}
a () {'a': 1, 'b': 2}
b (1, 2) {}
b (1,) {'b': 2}
b () {'a': 1, 'b': 2}
from decorator.
It is a by-product of a change in the implementation (now I am using the Signature
object instead of exec
). BTW, I regard the previous behavior to be correct (a
and b
are positional argument, not keyword arguments) and the new one to be wrong, but I have stopped fighting it.
from decorator.
The behavior that you wanted has been disabled by default because of backward compatibility issues (see the discussion in #104) but you can enable it by using the kwsyntax
flag (see the docs).
from decorator.
Thanks for the extra effort to retain this feature. Also for the follow up here.
from decorator.
Related Issues (20)
- 5.0.9: sphinx 4.x warnings HOT 3
- pylint warning W1113: keyword-arg-before-vararg HOT 1
- Regression in 5.1.0 - decorator.contextmanager no longer compat with contextlib.contextmanager HOT 4
- `FunctionMaker.create()` fails when given a function definition with a return type annotation. HOT 1
- Functionmaker.create (from doc example) fails with kwarg-only functions HOT 6
- `FunctionMaker.create` raises unexpected SyntaxError when return is present as substring for async function
- How to make `@decorator` with other decorators? HOT 3
- TypeError: missing 1 required positional argument HOT 1
- <=3.4.2 can't be installed with modern setuptools HOT 2
- Continuous fuzzing by way of OSS-Fuzz
- compatibility issue with kwargs HOT 2
- Not preserving low level signature metadada HOT 1
- decorator whether to support decorator partial functions?
- Confusion about license using HOT 1
- Making __name__ optional attribute of decorated-function if underlying function is missing it. HOT 3
- Compatibility with Python 3.12 - RuntimeError: There is no current event loop in thread 'MainThread'.
- Unable to pull 4.4.2 from the repo only pulls in 4.2.1 HOT 1
- [BUG] kwargs are not respected HOT 3
- Create SECURITY.md
- Create a decorator for both sync and async function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decorator.