Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4956: autodoc: Failed to extract document from a subclass of the class on mocked module #4995

Merged
merged 1 commit into from
May 21, 2018

Conversation

tk0miya
Copy link
Member

@tk0miya tk0miya commented May 20, 2018

Copy link
Member

@shimizukawa shimizukawa left a comment

Choose a reason for hiding this comment

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

LGTM!

# type: (Any, Any) -> Any
if len(args) == 3 and isinstance(args[1], tuple) and args[1][-1].__class__ is cls:
# subclassing MockObject
return type(args[0], (_MockObject,), args[2], **kwargs) # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

confirmed.

Copy link
Member

@shimizukawa shimizukawa left a comment

Choose a reason for hiding this comment

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

I've investigated for py2 error.

# subclassing MockObject
return type(args[0], (_MockObject,), args[2], **kwargs) # type: ignore
else:
return super().__new__(cls) # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

not works with py2. Is super(M, cls) correct?

            return super(M, cls).__new__(cls)  # type: ignore

test.py

from __future__ import print_function

class M(object):
    def __new__(cls, *args, **kwargs):
        print('new =>', cls, args, kwargs)
        if len(args) == 3 and isinstance(args[1], tuple) and args[1][-1].__class__ is cls:
            # subclassing MockObject
            return type(args[0], (M,), args[2], **kwargs)  # type: ignore
        else:
            return super(M, cls).__new__(cls)  # type: ignore

t1 = 'm = M(1, b=2)'
print('exec:', t1); exec(t1)

print('subclassing M')
class F(M):
    def __init__(self, a, b=0):
        self.a = a
        self.b = b

t2 = 'f = F(1, b=2)'
print('exec:', t2); exec(t2)


print('subclassing m')
class X(m):
    def __init__(self, a, b=0):
        self.a = a
        self.b = b

t3 = 'x = X(1, b=2)'
print('exec:', t3); exec(t3)

run with py3

$ python3 test.py 
exec: m = M(1, b=2)
new => <class '__main__.M'> (1,) {'b': 2}
subclassing M
exec: f = F(1, b=2)
new => <class '__main__.F'> (1,) {'b': 2}
subclassing m
new => <class '__main__.M'> ('X', (<__main__.M object at 0x1013b4400>,), {'__module__': '__main__', '__qualname__': 'X', '__init__': <function X.__init__ at 0x1013a98c8>}) {}
exec: x = X(1, b=2)
new => <class '__main__.X'> (1,) {'b': 2}

run with py2

$ python2 test.py 
exec: m = M(1, b=2)
new => <class '__main__.M'> (1,) {'b': 2}
subclassing M
exec: f = F(1, b=2)
new => <class '__main__.F'> (1,) {'b': 2}
subclassing m
new => <class '__main__.M'> ('X', (<__main__.M object at 0x102132c50>,), {'__module__': '__main__', '__init__': <function __init__ at 0x102135b18>}) {}
exec: x = X(1, b=2)
new => <class '__main__.X'> (1,) {'b': 2}

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. I'd forgotten the usage of super() in py2...

@tk0miya tk0miya force-pushed the 4956_subclass_of_mock_class branch from 6126dab to 72fbbc4 Compare May 21, 2018 11:54
@codecov
Copy link

codecov bot commented May 21, 2018

Codecov Report

Merging #4995 into 1.7 will increase coverage by 0.01%.
The diff coverage is 91.3%.

Impacted file tree graph

@@            Coverage Diff            @@
##              1.7   #4995      +/-   ##
=========================================
+ Coverage   81.99%     82%   +0.01%     
=========================================
  Files         282     288       +6     
  Lines       37681   38022     +341     
  Branches     5846    5901      +55     
=========================================
+ Hits        30895   31179     +284     
- Misses       5481    5529      +48     
- Partials     1305    1314       +9
Impacted Files Coverage Δ
tests/test_ext_autodoc_importer.py 100% <100%> (ø)
sphinx/ext/autodoc/importer.py 87.76% <71.42%> (-0.21%) ⬇️
sphinx/apidoc.py 0% <0%> (ø)
sphinx/__init__.py 57.5% <0%> (ø)
sphinx/errors.py 68.42% <0%> (ø)
sphinx/quickstart.py 0% <0%> (ø)
sphinx/search/__init__.py 95.11% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 713aa82...3080d24. Read the comment docs.

@tk0miya tk0miya force-pushed the 4956_subclass_of_mock_class branch from 72fbbc4 to 3080d24 Compare May 21, 2018 12:53
@@ -47,6 +55,10 @@ def __iter__(self):
# type: () -> None
pass

def __mro_entries__(self, bases):
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@shimizukawa shimizukawa left a comment

Choose a reason for hiding this comment

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

LGTM!!

@tk0miya tk0miya merged commit c2a245a into sphinx-doc:1.7 May 21, 2018
@tk0miya tk0miya deleted the 4956_subclass_of_mock_class branch May 21, 2018 13:23
@tk0miya
Copy link
Member Author

tk0miya commented May 21, 2018

Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants