Repository: sphinx-doc/sphinx

Issue:
Linking to implicit namespace package with :py:mod: not working
### Describe the bug

The [documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#role-py-mod) for :py:mod: says that it can also be used to link to packages. This seems to work for regular packages, but not implicit namespace packages.

### How to Reproduce

```
mkdir -p testcase/foo
cd testcase
touch foo/bar.py
python3 -m venv venv
source venv/bin/activate
pip install sphinx
mkdir source
echo 'import os; import sys; sys.path.insert(0, os.path.abspath("..")); extensions = ["sphinx.ext.autodoc"]' > source/conf.py
sphinx-apidoc -f --implicit-namespaces -o source foo
echo ':py:mod:`foo`' > source/index.rst
sphinx-build source build
xdg-open build/index.html
```

Notice how "foo" in the output is not a hyperlink.

On the other hand, if we turn foo into a regular package and re-run sphinx-apidoc / sphinx-build:

```
touch foo/__init__.py
sphinx-apidoc -f -o source foo
sphinx-build source build
xdg-open build/index.html
```

Then "foo" is now a hyperlink.

### Expected behavior

I expect "foo" in the output to be a link to `foo.html#foo-namespace`.

### Your project

See steps above

### Screenshots

_No response_

### OS

Ubuntu 20.04.3 LTS

### Python version

3.8.10

### Sphinx version

4.1.2

### Sphinx extensions

sphinx.ext.autodoc

### Extra tools

_No response_

### Additional context

_No response_

Discussion:
estan: @tk0miya Thanks for the very quick fix :+1: Can confirm this fixes the problem in my https://github.com/sphinx-doc/sphinx/issues/9608 test case.

Patch:
diff --git a/CHANGES b/CHANGES
index c7e781de8..5d31ebcef 100644
--- a/CHANGES
+++ b/CHANGES
@@ -25,6 +25,8 @@ Features added
 Bugs fixed
 ----------
 
+* #9608: apidoc: apidoc does not generate a module definition for implicit
+  namespace package
 * #9487: autodoc: typehint for cached_property is not shown
 * #9509: autodoc: AttributeError is raised on failed resolving typehints
 * #9518: autodoc: autodoc_docstring_signature does not effect to ``__init__()``
diff --git a/sphinx/templates/apidoc/package.rst_t b/sphinx/templates/apidoc/package.rst_t
index b7380e8a0..2229c311b 100644
--- a/sphinx/templates/apidoc/package.rst_t
+++ b/sphinx/templates/apidoc/package.rst_t
@@ -19,6 +19,10 @@
 {{- [pkgname, "package"] | join(" ") | e | heading }}
 {% endif %}
 
+{%- if is_namespace %}
+.. py:module:: {{ pkgname }}
+{% endif %}
+
 {%- if modulefirst and not is_namespace %}
 {{ automodule(pkgname, automodule_options) }}
 {% endif %}


Test patch:
diff --git a/tests/test_ext_apidoc.py b/tests/test_ext_apidoc.py
index d6c45c268..ff057f0d8 100644
--- a/tests/test_ext_apidoc.py
+++ b/tests/test_ext_apidoc.py
@@ -635,6 +635,8 @@ def test_namespace_package_file(tempdir):
     assert content == ("testpkg namespace\n"
                        "=================\n"
                        "\n"
+                       ".. py:module:: testpkg\n"
+                       "\n"
                        "Submodules\n"
                        "----------\n"
                        "\n"