Repository: sphinx-doc/sphinx

Issue:
python domain generates unexpected prefix
**Describe the bug**
python domain generates unexpected prefix

**To Reproduce**
```
.. py:class:: Foo

   .. py:method:: Foo.say(name)

   .. py:method:: FooBar.say(name)
```

The former is displayed as `say()`, without prefix. But another one is displayed as `Bar.say()`. It is very strange to me.

![スクリーンショット 2019-03-23 18 18 07](https://user-images.githubusercontent.com/748828/54864245-083b9100-4d98-11e9-8714-8777a7006f1c.png)

**Expected behavior**

The second example will be displayed as `FooBar.say()`.

**Environment info**
- OS: Mac
- Python version: 3.6.5
- Sphinx version: 2.0.0b2


Discussion:

Patch:
diff --git a/CHANGES b/CHANGES
index 717517e70..a7f2db423 100644
--- a/CHANGES
+++ b/CHANGES
@@ -16,6 +16,8 @@ Features added
 Bugs fixed
 ----------
 
+* #6196: py domain: unexpected prefix is generated
+
 Testing
 --------
 
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py
index 6c4030a6c..73d6d2c11 100644
--- a/sphinx/domains/python.py
+++ b/sphinx/domains/python.py
@@ -262,7 +262,8 @@ def handle_signature(self, sig, signode):
         classname = self.env.ref_context.get('py:class')
         if classname:
             add_module = False
-            if name_prefix and name_prefix.startswith(classname):
+            if name_prefix and (name_prefix == classname or
+                                name_prefix.startswith(classname + ".")):
                 fullname = name_prefix + name
                 # class name is given again in the signature
                 name_prefix = name_prefix[len(classname):].lstrip('.')


Test patch:
diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py
index 87c310982..fb6e70914 100644
--- a/tests/test_domain_py.py
+++ b/tests/test_domain_py.py
@@ -273,3 +273,20 @@ def test_pydata_signature(app):
                                   desc_content)]))
     assert_node(doctree[1], addnodes.desc, desctype="data",
                 domain="py", objtype="data", noindex=False)
+
+
+def test_pyobject_prefix(app):
+    text = (".. py:class:: Foo\n"
+            "\n"
+            "   .. py:method:: Foo.say\n"
+            "   .. py:method:: FooBar.say")
+    doctree = restructuredtext.parse(app, text)
+    assert_node(doctree, (addnodes.index,
+                          [desc, ([desc_signature, ([desc_annotation, "class "],
+                                                    [desc_name, "Foo"])],
+                                  [desc_content, (addnodes.index,
+                                                  desc,
+                                                  addnodes.index,
+                                                  desc)])]))
+    assert doctree[1][1][1].astext().strip() == 'say'           # prefix is stripped
+    assert doctree[1][1][3].astext().strip() == 'FooBar.say'    # not stripped