From 97e994700bc98f1d7376c184efda87b4919bdea0 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 5 Aug 2021 11:32:20 +0800 Subject: [PATCH] compiler: turn __repr__ into __str__ when sphinx is used. Closes #741 --- artiq/compiler/types.py | 21 +++++++++++++++++++++ doc/manual/conf.py | 7 +++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/artiq/compiler/types.py b/artiq/compiler/types.py index 78364101b..b5a220aeb 100644 --- a/artiq/compiler/types.py +++ b/artiq/compiler/types.py @@ -3,6 +3,7 @@ The :mod:`types` module contains the classes describing the types in :mod:`asttyped`. """ +import builtins import string from collections import OrderedDict from . import iodelay @@ -96,6 +97,8 @@ class TVar(Type): return self.find().fold(accum, fn) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) if self.parent is self: return "" % id(self) else: @@ -142,6 +145,8 @@ class TMono(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TMono(%s, %s)" % (repr(self.name), repr(self.params)) def __getitem__(self, param): @@ -190,6 +195,8 @@ class TTuple(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TTuple(%s)" % repr(self.elts) def __eq__(self, other): @@ -268,6 +275,8 @@ class TFunction(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TFunction({}, {}, {})".format( repr(self.args), repr(self.optargs), repr(self.ret)) @@ -361,6 +370,8 @@ class TRPC(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TRPC({})".format(repr(self.ret)) def __eq__(self, other): @@ -398,6 +409,8 @@ class TBuiltin(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.{}({})".format(type(self).__name__, repr(self.name)) def __eq__(self, other): @@ -458,6 +471,8 @@ class TInstance(TMono): self.constant_attributes = set() def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TInstance({}, {})".format( repr(self.name), repr(self.attributes)) @@ -473,6 +488,8 @@ class TModule(TMono): self.constant_attributes = set() def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TModule({}, {})".format( repr(self.name), repr(self.attributes)) @@ -512,6 +529,8 @@ class TValue(Type): return fn(accum, self) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) return "artiq.compiler.types.TValue(%s)" % repr(self.value) def __eq__(self, other): @@ -570,6 +589,8 @@ class TDelay(Type): return not (self == other) def __repr__(self): + if getattr(builtins, "__in_sphinx__", False): + return str(self) if self.duration is None: return "<{}.TIndeterminateDelay>".format(__name__) elif self.cause is None: diff --git a/doc/manual/conf.py b/doc/manual/conf.py index 997801987..509816e80 100644 --- a/doc/manual/conf.py +++ b/doc/manual/conf.py @@ -20,11 +20,10 @@ from unittest.mock import Mock import sphinx_rtd_theme -# Hack-patch Sphinx so that ARTIQ-Python types are correctly printed +# Ensure that ARTIQ-Python types are correctly printed # See: https://github.com/m-labs/artiq/issues/741 -from sphinx.ext import autodoc -from sphinx.util import inspect -autodoc.repr = inspect.repr = str +import builtins +builtins.__in_sphinx__ = True # we cannot use autodoc_mock_imports (does not help with argparse)