# Leading dots in class references – why and why not?

**URL:** <https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319>\
**Category:** Documentation\
**Tags:** help\
**Created:** [January 27, 2024, 6:32pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319 "2024-01-27T18:32:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [January 27, 2024, 6:32pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/1 "2024-01-27T18:32:29Z")

</div>

Looking at `Doc/library/datetime.rst` I see this:

```python
:class:`date`, :class:`.datetime`, and :class:`.time` objects all support a
``strftime(format)`` method, to create a string representing the time under the
control of an explicit format string.

```

What does the leading dot in front of the `datetime` and `time` classes mean, and why isn’t the `date` class referenced so adorned?

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [January 27, 2024, 6:44pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/2 "2024-01-27T18:44:59Z")

</div>

See [The Python Domain — Sphinx documentation](https://www.sphinx-doc.org/en/master/usage/domains/python.html#cross-referencing-python-objects)

> Normally, names in these roles are searched first without any further qualification, then with the current module name prepended, then with the current module and class name (if any) prepended. If you prefix the name with a dot, this order is reversed. For example, in the documentation of Python’s [`codecs`](https://docs.python.org/3/library/codecs.html#module-codecs) module, `:py:func:`open`` always refers to the built-in function, while `:py:func:`.open`` refers to [`codecs.open()`](https://docs.python.org/3/library/codecs.html#codecs.open).

This is to avoid `:class:`time`` making a reference to the `time` module, and `:class:`datetime`` to the datetime module (instead of the class). TBH, I’m not sure why Sphinx is happy to insert a reference to a Python module for a `:class:` role.

---

<div class="post-metadata">

**Author:** ![merwok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/merwok/32/29_2.png) [@merwok](https://discuss.python.org/u/merwok)\
**Post date:** [January 28, 2024, 6:56pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/3 "2024-01-28T18:56:18Z")

</div>

That’s because mod, class, func, meth, data are all really «look this name up in the names index, make a link and display as code»! In other words not type-checked or limited. `:func:`os`` will work.

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [January 28, 2024, 8:10pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/4 "2024-01-28T20:10:25Z")

</div>

> [@merwok](#):
>
> `:func:`os` will work.

So, no difference, not even in indexes? `:func:`os` `wouldn’t cause `os` to turn up as a function in a generated function index? (I don’t know if that’s a thing in Sphinx…)

---

<div class="post-metadata">

**Author:** ![merwok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/merwok/32/29_2.png) [@merwok](https://discuss.python.org/u/merwok)\
**Post date:** [January 28, 2024, 8:25pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/5 "2024-01-28T20:25:56Z")

</div>

That example was for a reference. At the definition point, markup like ` .. module:: os` will be used, so that the index correctly calls it a module.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 28, 2024, 9:09pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/6 "2024-01-28T21:09:04Z")

</div>

IMO, we should instead spell these out:

```rst
:func:`~module.function` # <= like this
:func:`.function` # <= not like this

```

It is too easy to not see the leading dot; I’ve reworked docs where I’ve accidentally introduced the wrong link, because I did not see the leading dot in the first place. The explicit variant is IMO better.

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [January 28, 2024, 9:37pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/7 "2024-01-28T21:37:54Z")

</div>

Agreed, that’s just for the small number of experienced folks who know the trick; its potentially even more confusing and non-obvious to folks who don’t (as was the case here); even I didn’t know about it until relatively recently, well after I knew pretty much all the other ins and outs of the xref roles.

FWIW I get 302 hits for the use of `.` in this manner (`:(class|func|meth|attr|data|const|mod):`.[a-zA-Z]`) in `.rst`files under`Doc/ (a good deal more than I expected, actually), `; by contrast I get 4778 hits for the much more common use of `~` in this manner (`:(class|func|meth|attr|data|const|mod):`~[a-zA-Z]`)

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [January 28, 2024, 10:40pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/8 "2024-01-28T22:40:38Z")

</div>

It does not work if you want to refer to `classname.methname`. You need to write more verbose `:meth:`classname.methname <module.classname.methname>`` if you do not want to write `:meth:`.classname.methname``. I do not think that the former is much clearer, especially if you use several such references per paragraph.

`.` is a shortcut, which allows you to to use more laconic markup if it is applicable. It is can only be used in limited cases, but when it can be used, it makes a difference.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 28, 2024, 11:41pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/9 "2024-01-28T23:41:12Z")

</div>

Yes, so a devguide recommendation would be welcome; one for the general case (spell it out), and one for the special case (use the shortcut).

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [January 29, 2024, 12:27am UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/10 "2024-01-29T00:27:58Z")

</div>

Sounds like there’s more than one way to do it. Not too Pythonic. Maybe the problematic construction could be discouraged/deprecated? (Take this with a grain of salt. I have essentially no experience with Sphinx, and not much more with ReST.)

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [January 29, 2024, 4:44am UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/11 "2024-01-29T04:44:57Z")

</div>

To my surprise testing it just now, a leading `.` doesn’t just trigger xref lookup to search local names before global ones, but actually does something arguably a lot more interesting, theoretically useful, yet unintuitive: it searches under _all_ modules for objects that match.

This mean that in e.g. the What’s New you can do :meth:`.ClassName.method_name` without using `.. currentmodule`, and the lookup will actually work (which is used a handful of places in the 3.4 What’s New)—_if_ there happens to be only one `ClassName.method_name` in the entire docs, which is certainly not always the case. This even works with, e.g., `:meth:`.methodname``—it will match a method named `methodname` under _any_ class in _any_ module (though of course, not terribly useful given the ambiguity and likelihood of multiple matches).

However, mo’ heuristics, mo’ problems—this capability actually has _more_ issues than the previous, despite being nominally more powerful:

- This is arguably even _more_ surprising (as apparently no one here, including myself, was even aware of it until now)
- A user intending to link to something in the current module may silently end up linking something completely different with no warning, if they mistype a name, it doesn’t exist/isn’t documented in the current module, or they misunderstand the complex lookup rules around this feature, and the name exists anywhere else in the docs.
- It only works if there happens to be exactly one class of that name with that method anywhere in any module, in the present and the future, or it may unpredictably link to the wrong method (although a warning will also be emitted in this case, if nitpicky mode is enabled).

Therefore, given that one-off instances of needing this can be replaced with the slightly more verbose but explicit and clear form, and the use cases with multiple that really benefit (e.g. What’s New) can just use `.. currentmodule`, which has the same conciseness benefit while being more explicit and without all the caveats and complexity, I’m still not sure its worth using for us.

> [@storchaka](#):
>
> It does not work if you want to refer to `classname.methname`. You need to write more verbose `:meth:`classname.methname \<module.classname.methname\>` if you do not want to write `:meth:`.classname.methname`.

Yeah, but this is only relevant for the extremely narrow corner case where all of the following are true:

- A class has the same name as its own module
- That class has a method with the same name as a top-level function of that module
- The documentation referencing the method is inside the module but outside the class
- It wishes to reference the class’ method rather than module’s, AND
- The writer wishes the text to display the (potentially confusing) `classname.methodname`, rather than either `methodname` or `modulename.classname.methodname`.

And of course, it is for those few cases that clarity and explicitness is most beneficial for both the reader and the writer, and for which the total verbosity cost is small by comparison.

In total, there are only 12 hits in `.rst` files under `/Docs` for `:(class|func|meth|attr|data|const|mod):`(\.\w+){2,}``, which matches all xref roles that use a leading `.` for names with two or more dotted components. 8 of those are the aforementioned usages in the 3.4 What’s New (which could be replaced by `currentmodule`, with a substantial net increase to overall conciseness while improving explicitness), and in any case don’t fall under the specific case quoted.

The remaining four are all in `datetime.rst`, and of them:

- In `:meth:`.date.strftime`` the `.` isn’t actually necessary, since there is no `date` module (much less one with a top-level `strftime` function), so `:meth:`date.strftime`` will resolve as intended to the class method.
- In `:meth:`.datetime.timetuple``, there is no top-level `timetuple` in the `datetime` module, so likewise `:meth:`datetime.timetuple`` resolves as intended.
- In `:meth:`.datetime.strftime` `, again there’s no top-level `strftime` in the `datetime` module, so `:meth:`datetime.strftime`` resolves to the class method.
- In `:meth:`.time.strftime``, there _is_ a `time` module with a `strftime` method, so without the prepended `.`, this _would_ resolve to the wrong object (unless the lookup takes into account the specified role, which AFAIK it currently does not. Of course, it can be replaced with the more verbose but explicit `:meth:`time.strftime <datetime.time.strftime>``, which would also avoid

So, in total, there’s only a single instance in the entire Python documentation where this corner case actually occurs, so it doesn’t seem worth using potentially confusing syntax over the more explicit form just for that reason.

> [@storchaka](#):
>
> I do not think that the former is much clearer, especially if you use several such references per paragraph.

Per [agreed convention](https://github.com/python/docs-community/issues/52), normally only the first cross reference to an object in a paragraph (or equivalent information unit) need be resolved, so this would only occur at most once per paragraph; the rest would simply be :meth:`!classname.methname` regardless.

> [@storchaka](#):
>
> `.` is a shortcut, which allows you to to use more laconic markup if it is applicable. It is can only be used in limited cases, but when it can be used, it makes a difference.

To be fair, at least conceptually, `.spam` is really more of a modifier to the implicit lookup behavior than a shortcut—particularly given the above findings. And it actually does have _some_ advantages in terms of being more explicit and avoiding some silent mistakes than the _unqualified_ un-`.`-prefixed versions, when used inside a module to refer to, e.g., a method of a class of the same name—in the `datetime` case, using e.g. `.date.strftime` and friends instead of just `date.starftime`, etc., which will always match the class method and never any module-level function, and makes it clear that is what is intended.

This would be a clear improvement, except that as a direct result of the above behavior, if no `date.strftime` is present under the `datetime` module, the `.`-prefixed version will search for and match _any_ `date.strftime` method under _any_ module in the docs, instead of simply stopping resolution and giving a clear warning. Therefore, its a mixed bag as to whether that’s really better, whereas only the explicit, fully-qualified version avoids both these sets of issues.

> [@smontanaro](#):
>
> Sounds like there’s more than one way to do it. Not too Pythonic. Maybe the problematic construction could be discouraged/deprecated?

If there is enough consensus we could maybe consider add an opt-in sphinx-lint rule for it, due to the confusion, ambiguity and possible unexpected behavior. But I’m not sure how high on the list that would be, compared to other priorities.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 29, 2024, 11:08am UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/12 "2024-01-29T11:08:07Z")

</div>

> [@CAM-Gerlach](#):
>
> So, in total, there’s only a single instance in the entire Python documentation where this corner case actually occurs, so it doesn’t seem worth using potentially confusing syntax over the more explicit form just for that reason.

Totally agree. Let’s instead focus on unambiguous markup syntax.

---

<div class="post-metadata">

**Author:** ![smontanaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/smontanaro/32/1389_2.png) [@smontanaro](https://discuss.python.org/u/smontanaro)\
**Post date:** [January 29, 2024, 2:48pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/13 "2024-01-29T14:48:22Z")

</div>

> [@CAM-Gerlach](#):
>
> Per [agreed convention](https://github.com/python/docs-community/issues/52), normally only the first cross reference to an object in a paragraph

Is there a “Best Sphinx Practices” or “Established Sphinx Conventions” document which highlights the best way to do stuff? Your reference is to a GitHub issue, not the most obvious place people will look.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 29, 2024, 3:01pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/14 "2024-01-29T15:01:58Z")

</div>

> [@smontanaro](#):
>
> Is there a “Best Sphinx Practices” or “Established Sphinx Conventions” document which highlights the best way to do stuff?

There’s a dedicated pages over [in the devguide](https://devguide.python.org/documentation/markup/).

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [January 29, 2024, 10:00pm UTC](https://discuss.python.org/t/leading-dots-in-class-references-why-and-why-not/44319/15 "2024-01-29T22:00:10Z")

</div>

And also the [Style Guide page](https://devguide.python.org/documentation/style-guide/) for general style. To note, not quite everything has made it over there yet—I’m not sure exactly where we formally added that specific suggestion (though I seem to recall it somewhere. PRs welcome!
