Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type aliases in type hints are not preserved

My code (reduced to just a few lines for this demo) looks like this:

AgentAssignment = List[int]

def assignment2str(assignment: AgentAssignment):    
    pass

The produced html documentation has List[int] as the type hint. This seems to be a resolved issue (#6518), but I am still running into it in 2022 with Sphinx version 5.1.1 (and Python 3.8.2). What am I missing?

like image 772
AlwaysLearning Avatar asked Oct 29 '25 19:10

AlwaysLearning


1 Answers

So, one needs to add at the beginning of the file (yes, before all other imports):

from __future__ import annotations

Then in conf.py:

autodoc_type_aliases = {'AgentAssignment': 'AgentAssignment'}

Not that this identity-transformation dictionary makes any sense to me, but it did the trick...

like image 166
AlwaysLearning Avatar answered Nov 01 '25 10:11

AlwaysLearning