Repository: sphinx-doc/sphinx

Issue:
Formatting issues with autodoc_typehints='none'
**Describe the bug**

When using `autodoc_typehints='none'`, I see two issues currently:

1. When an annotated parameter has a default value, spaces are inserted around `=` where they are not for unannotated parameters. Consider:

   ```python
   def foo(x=True, y: bool = True, z = True):
       return x and y and z
   ```

   The result looks like:

   **foo**(*x=True, y = True, z=True*)

2. Return types are not removed. Consider:

   ```python
   def bar(x: int) -> int:
       return x * 2
   ```

   The result looks like:

   **bar**(*x*) -> int

**To Reproduce**
Steps to reproduce the behavior:

Use the above examples with the `autodoc_typehints='none'` option, or with the attached project (see below).

**Expected behavior**

I expect the annotated and unannotated parameters to be rendered similarly and for return type annotations to be removed as well, as if they were not there originally.

**Your project**

See attached [foobar.zip](https://github.com/sphinx-doc/sphinx/files/3406054/foobar.zip)

**Environment info**
- OS: [Pop!_OS 18.10]
- Python version: 3.6.8
- Sphinx version: 2.1.2
- Sphinx extensions:  [sphinx.ext.autodoc]

**Additional context**

- #6361 
- #5868 
- https://github.com/agronholm/sphinx-autodoc-typehints/pull/78



Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`2.0@4732ec5`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592/graphs/tree.svg?width=650&token=VFlFSELbkw&height=150&src=pr)](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##             2.0    #6592   +/-   ##
======================================
  Coverage       ?   83.78%           
======================================
  Files          ?      269           
  Lines          ?    40880           
  Branches       ?     5979           
======================================
  Hits           ?    34253           
  Misses         ?     5303           
  Partials       ?     1324
```


| [Impacted Files](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_ext\_autodoc\_configs.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9leHRfYXV0b2RvY19jb25maWdzLnB5) | `97.56% <ø> (ø)` | |
| [tests/test\_util\_inspect.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF91dGlsX2luc3BlY3QucHk=) | `92.78% <100%> (ø)` | |
| [sphinx/util/inspect.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592/diff?src=pr&el=tree#diff-c3BoaW54L3V0aWwvaW5zcGVjdC5weQ==) | `72.43% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=footer). Last update [4732ec5...a4494d5](https://codecov.io/gh/sphinx-doc/sphinx/pull/6592?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGES b/CHANGES
index 391a2858a..d63aa31c9 100644
--- a/CHANGES
+++ b/CHANGES
@@ -50,6 +50,7 @@ Bugs fixed
   ``__init__()`` and ``__new__()``
 * #6574: autodoc: :confval:`autodoc_member_order` does not refer order of
   imports when ``'bysource'`` order
+* #6589: autodoc: Formatting issues with autodoc_typehints='none'
 * #6498: autosummary: crashed with wrong autosummary_generate setting
 * #6507: autosummary: crashes without no autosummary_generate setting
 * #6511: LaTeX: autonumbered list can not be customized in LaTeX
diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py
index d195dea0b..d6d89a6b1 100644
--- a/sphinx/util/inspect.py
+++ b/sphinx/util/inspect.py
@@ -428,7 +428,7 @@ def format_args(self, show_annotation: bool = True) -> str:
                         arg.write(': ')
                         arg.write(self.format_annotation(param.annotation))
                 if param.default is not param.empty:
-                    if param.annotation is param.empty:
+                    if param.annotation is param.empty or show_annotation is False:
                         arg.write('=')
                         arg.write(object_description(param.default))
                     else:
@@ -444,7 +444,7 @@ def format_args(self, show_annotation: bool = True) -> str:
             args.append(arg.getvalue())
             last_kind = param.kind
 
-        if self.return_annotation is inspect.Parameter.empty:
+        if self.return_annotation is inspect.Parameter.empty or show_annotation is False:
             return '(%s)' % ', '.join(args)
         else:
             if 'return' in self.annotations:


Test patch:
diff --git a/tests/test_ext_autodoc_configs.py b/tests/test_ext_autodoc_configs.py
index 1a7224b11..b9a92fb04 100644
--- a/tests/test_ext_autodoc_configs.py
+++ b/tests/test_ext_autodoc_configs.py
@@ -501,15 +501,15 @@ def test_autodoc_typehints_none(app):
         '.. py:module:: target.typehints',
         '',
         '',
-        '.. py:class:: Math(s, o = None)',
+        '.. py:class:: Math(s, o=None)',
         '   :module: target.typehints',
         '',
         '   ',
-        '   .. py:method:: Math.incr(a, b = 1) -> int',
+        '   .. py:method:: Math.incr(a, b=1)',
         '      :module: target.typehints',
         '   ',
         '',
-        '.. py:function:: incr(a, b = 1) -> int',
+        '.. py:function:: incr(a, b=1)',
         '   :module: target.typehints',
         ''
     ]
diff --git a/tests/test_util_inspect.py b/tests/test_util_inspect.py
index 0681352e3..047f36ac2 100644
--- a/tests/test_util_inspect.py
+++ b/tests/test_util_inspect.py
@@ -285,6 +285,10 @@ def test_Signature_annotations():
     sig = inspect.Signature(Node.__init__).format_args()
     assert sig == '(self, parent: Optional[Node]) -> None'
 
+    # show_annotation is False
+    sig = inspect.Signature(f7).format_args(show_annotation=False)
+    assert sig == '(x=None, y={})'
+
 
 def test_safe_getattr_with_default():
     class Foo: