This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author michael.foord
Recipients cbelu, cjw296, mariocj89, michael.foord, xtreak
Date 2019-04-01.10:21:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <5DDE12CC-71FF-4D44-821F-C616D8F841BE@voidspace.org.uk>
In-reply-to <1554052062.13.0.86574463435.issue30587@roundup.psfhosted.org>
Content
Spec objects are currently dumb. It would be a new feature to add signature validation to them. 

I think it would be a useful feature though as currently autospec sort of obsoletes spec objects whilst being more heavyweight and harder to use. 

I think it would appropriate to add to 3.8 but not to 3.7. 

Compatibility with existing tests is the issue. The obvious answer is a flag to turn it on/off but that adds complexity to the API. 

My gut feeling is that spec objects are far more commonly used in situations where validation would be useful than a detriment. 

> On 31 Mar 2019, at 18:07, Karthikeyan Singaravelan <report@bugs.python.org> wrote:
> 
> 
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
> 
> I am slightly concerned if spec should gain more responsibility than just validating attribute access which is mentioned in the docs. With the linked PR spec below would also validate the signature which is not done in Python 3.7 so this might break code for someone who only wants to validate access attribute access and not signature of the methods. It seems the PR also adds autospec argument to Mock that is currently not supported though spec and spec_set are supported.
> 
> from unittest import mock
> 
> def foo(lish):
>    pass
> 
> mock_foo = mock.Mock(spec=foo)
> mock_foo(1, 2)
> 
> try:
>    mock_foo.non_existent
> except AttributeError:
>    print("raises AttributeError for non-existent attribute")
> 
> # 3.7
> 
> ➜  cpython git:(pr_1982) python3.7 /tmp/foo.py
> raises AttributeError for non-existent attribute
> 
> # With PR
> 
> ➜  cpython git:(pr_1982) ./python.exe /tmp/foo.py
> Traceback (most recent call last):
>  File "/tmp/foo.py", line 7, in <module>
>    mock_foo(1, 2)
>  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 1009, in __call__
>    _mock_self._mock_check_sig(*args, **kwargs)
>  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 103, in checksig
>    sig.bind(*args, **kwargs)
>  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", line 3016, in bind
>    return args[0]._bind(args[1:], kwargs)
>  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", line 2937, in _bind
>    raise TypeError('too many positional arguments') from None
> TypeError: too many positional arguments
> 
> ----------
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue30587>
> _______________________________________
History
Date User Action Args
2019-04-01 10:21:29michael.foordsetrecipients: + michael.foord, cjw296, mariocj89, cbelu, xtreak
2019-04-01 10:21:29michael.foordlinkissue30587 messages
2019-04-01 10:21:28michael.foordcreate