aliles / funcsigs Goto Github PK
View Code? Open in Web Editor NEWPython function signatures package for Python 2.6, 2.7 and 3.2+
Home Page: http://pypi.python.org/pypi/funcsigs
License: Other
Python function signatures package for Python 2.6, 2.7 and 3.2+
Home Page: http://pypi.python.org/pypi/funcsigs
License: Other
from funcsigs import signature
from functools import partial
f = partial(issubclass, bool)
signature(f)
yields
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Miniconda3\envs\tools_py2\lib\site-packages\funcsigs\__init__.py", line 108, in signature
sig = signature(obj.func)
File "C:\Miniconda3\envs\tools_py2\lib\site-packages\funcsigs\__init__.py", line 185, in signature
raise ValueError(msg)
ValueError: no signature found for builtin function <built-in function issubclass>
I recently updated to funcsigs 1.0 via the conda package manager and found that the documentation provided is for version 0.4.
I have a method like so:
class F:
def f(a, self):
pass
signature(partial(F.f, None)).bind(self=10)
fails, due to receiving self too many times.
The function bind() can't be defined with self as its own parameter - it conflicts with binds that pass self in. Instead it needs to take _args, *_kwargs, and pull self out of args[0].
I'll throw up a patch in alittle, for now (in mock), I'm monkeypatching to fix it.
Basically trying to pickle a signature pickle.dumps(signature(my_func))
leads to :
File "/usr/lib/python2.7/pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 77, in _reduce_ex
raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled
It would be useful to be able to serialize function signatures. Although I don't know what python 3.X does about this and if this is in the scope of funcsigs to implement this...
Currently it is not possible to install funcsigs using pip install.
The problem is caused by this line:
extras_require = {
':python_version<"2.7"': ['ordereddict'], },
error in funcsigs setup command: Invalid environment marker: python_version<"2.7"
Please remove the extra colon from the start of the string.
Hello, my name is Joel.
I've recently opened an issue in scikit-learn, and it turns out the issue has to do with funcsigs.
My question is:
Can the signature() method in funcsigs.py be used with a cython function (cyfunction object)?
As you can see in the other issue, I'm using python 2.7
Thanks!
http://bugs.python.org/issue23764 highlighted that one of the useful features inspect.signature offers over the old APIs is correctly following wrapper chains.
Python 3.4 split that feature out into an independently usable component: https://docs.python.org/3/library/inspect.html#inspect.unwrap
There are some other features potentially worth backporting from 3.4 (like the inspect.signature based inspect.getargspec and inspect.getfullargspec), so a full rebase may even be worthwhile.
Just thought I'd help other weary travelers.
======================================================================
ERROR: test_signature_on_callable_objects (tests.test_inspect.TestSignatureObject)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/var/tmp/portage/dev-python/funcsigs-0.4/work/funcsigs-0.4/tests/test_inspect.py", line 508, in test_signature_on_callable_objects
self.assertEqual(self.signature(Foo()),
File "/var/tmp/portage/dev-python/funcsigs-0.4/work/funcsigs-0.4/tests/test_inspect.py", line 17, in signature
sig = inspect.signature(func)
File "/var/tmp/portage/dev-python/funcsigs-0.4/work/funcsigs-0.4/funcsigs/__init__.py", line 176, in signature
raise ValueError('callable {0!r} is not supported by signature'.format(obj))
ValueError: callable <tests.test_inspect.Foo object at 0x0000000001405948> is not supported by signature
======================================================================
ERROR: test_signature_on_class (tests.test_inspect.TestSignatureObject)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<string>", line 7, in test_signature_on_class
File "/var/tmp/portage/dev-python/funcsigs-0.4/work/funcsigs-0.4/tests/test_inspect.py", line 17, in signature
sig = inspect.signature(func)
File "/var/tmp/portage/dev-python/funcsigs-0.4/work/funcsigs-0.4/funcsigs/__init__.py", line 176, in signature
raise ValueError('callable {0!r} is not supported by signature'.format(obj))
ValueError: callable <class 'tests.test_inspect.C'> is not supported by signature
----------------------------------------------------------------------
Trying to run the tests directly (without using the coverage
command) currently fails because unittest.begin()
is not a valid method. This should be replaced with with unittest.main()
.
Fixing this (and running tests standalone) will break one of the formatannotation
tests, as the expected result depends on how the tests are run.
<funcsigs.Signature object at 0x7fcc113c7dd0>, args = ('hello',), kwargs = {}, partial = False
def _bind(self, args, kwargs, partial=False):
'''Private method. Don't use directly.'''
arguments = OrderedDict()
parameters = iter(self.parameters.values())
parameters_ex = ()
arg_vals = iter(args)
if partial:
# Support for binding arguments to 'functools.partial' objects.
# See 'functools.partial' case in 'signature()' implementation
# for details.
for param_name, param in self.parameters.items():
if (param._partial_kwarg and param_name not in kwargs):
# Simulating 'functools.partial' behavior
kwargs[param_name] = param.default
while True:
# Let's iterate through the positional arguments and corresponding
# parameters
try:
arg_val = next(arg_vals)
except StopIteration:
# No more positional arguments
try:
param = next(parameters)
except StopIteration:
# No more parameters. That's it. Just need to check that
# we have no `kwargs` after this while loop
break
else:
if param.kind == _VAR_POSITIONAL:
# That's OK, just empty *args. Let's start parsing
# kwargs
break
elif param.name in kwargs:
if param.kind == _POSITIONAL_ONLY:
msg = '{arg!r} parameter is positional only, ' \
'but was passed as a keyword'
msg = msg.format(arg=param.name)
raise TypeError(msg)
parameters_ex = (param,)
break
elif (param.kind == _VAR_KEYWORD or
param.default is not _empty):
# That's fine too - we have a default value for this
# parameter. So, lets start parsing `kwargs`, starting
# with the current parameter
parameters_ex = (param,)
break
else:
if partial:
parameters_ex = (param,)
break
else:
msg = '{arg!r} parameter lacking default value'
msg = msg.format(arg=param.name)
raise TypeError(msg)
else:
# We have a positional argument to process
try:
param = next(parameters)
except StopIteration:
raise TypeError('too many positional arguments')
else:
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
# Looks like we have no parameter for this positional
# argument
> raise TypeError('too many positional arguments')
E TypeError: too many positional arguments
changing the positional arguments to keyword arguments resolves this issue, but surely funcsigs should work with multiple positional arguments?
funcsigs.signature() treats unbound methods as any other method, despite m.self being None.
The attached pull requests adds a test case and fix. I've used a positional-only parameter because of this behaviour:
>>> class Test(object):
... def method(self, *args, **kwargs):
... return self, args, kwargs
...
>>> Test.method(Test())
(<__main__.Test object at 0x2c9fad0>, (), {})
>>> Test.method(self=Test())
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unbound method method() must be called with Test instance as first argument
(got nothing instead)
I noticed that there is a disrepancy between the source on pypi.python.org and github.com. This is a little worrying, where is the source being version controlled if not here, the canonical source?
It links to https://github.com/alies/funcsigs/issues instead of https://github.com/aliles/funcsigs/issues
This is particularly a problem because the conda package manager won't allow you to use funcsigs 1.0 with python 2.7 because ordereddict cannot be installed for version 2.7. Setuptools fails with an error similar to this:
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 2912, in
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 2898, in _call_aside
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 2925, in _initialize_master_working_set
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 642, in _build_master
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 943, in require
File "/home/user/miniconda3/pkgs/setuptools-20.2.2-py27_0/lib/python2.7/site-packages/setuptools-20.2.2-py2.7.egg/pkg_resources/init.py", line 830, in resolve
pkg_resources.DistributionNotFound: The 'ordereddict' distribution was not found and is required by funcsigs
Attempting to explicitly install ordereddict with the conda package manager fails like so:
-bash-4.2$ conda install ordereddict
Fetching package metadata: ....
Solving package specifications: .......
Error: Unsatisfiable package specifications.
Generating hint:
[ COMPLETE ]|################################################| 100%
Hint: the following packages conflict with each other:
Use 'conda info ordereddict' etc. to see the dependencies for each package.
Presumably it should be possible to only require ordereddict for python versions < 2.6?
Hello :)
I'm using Sentry which uses Mock which uses this package.
I used to run pip wheel -r requirements.txt
and then pip install --no-index --use-wheel --find-links=WHEEL_DIR -r requirements.txt
and it worked fine for older versions. But now that this package emerged as a dependency, it no longer works.
I deleted the --no-index
flag and the installation works for now. Would be good to skip the index though.
The issue URL on PyPI at https://pypi.python.org/pypi/funcsigs points to https://github.com/alies/funcsigs/issues, which is a 404.
It should be https://github.com/aliles/funcsigs/issues - note it's missing a single lowercase "L", which is kind of hard to spot.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.