From d73d271a4fdd163a564772ff5fa31198c2a6ab08 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Mon, 13 Feb 2023 13:19:13 -0500 Subject: [PATCH] Fixed pydown to understand imported symbols. In order to import typing.Union into the module namespace, I had to fix pydown to recognize Union as a defined symbol. --- demo/python/astronomy.py | 3 ++- generate/pydown/pydown.py | 12 ++++++++++++ generate/template/astronomy.py | 3 ++- source/python/astronomy/astronomy.py | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/demo/python/astronomy.py b/demo/python/astronomy.py index 74bdd703..fbd1003d 100644 --- a/demo/python/astronomy.py +++ b/demo/python/astronomy.py @@ -36,6 +36,7 @@ import datetime import enum import re import abc +from typing import Union def _cbrt(x): if x < 0.0: @@ -728,7 +729,7 @@ class Time: such as the orbits of planets around the Sun, or the Moon around the Earth. Historically, Terrestrial Time has also been known by the term *Ephemeris Time* (ET). """ - def __init__(self, ut, tt = None): + def __init__(self, ut : Union[float, str], tt = None): if isinstance(ut, str): # Undocumented hack, to make repr(time) reversible. other = Time.Parse(ut) diff --git a/generate/pydown/pydown.py b/generate/pydown/pydown.py index dc1e5fda..89c7a63a 100755 --- a/generate/pydown/pydown.py +++ b/generate/pydown/pydown.py @@ -362,6 +362,17 @@ def ConstantsMd(inPythonFileName): clist = [] with open(inPythonFileName) as infile: for line in infile: + # Consider symbols like Union defined in lines like the following: + # "from typing import Union" + tokens = line.split() + if len(tokens) > 3 and tokens[0] == 'from' and tokens[2] == 'import': + for name in tokens[3:]: + if name != ',': + documentedSymbolSet.add(name) + continue + + # Consider symbols like KM_PER_AU defined in lines like the following: + # "KM_PER_AU = 1.4959787069098932e+8 # The number of kilometers per astronomical unit." parts = line.split('#') if len(parts) == 2: code = parts[0].strip() @@ -373,6 +384,7 @@ def ConstantsMd(inPythonFileName): symbol = tokens[0] clist.append((symbol, codeText, doc)) documentedSymbolSet.add(symbol) + continue for (symbol, code, doc) in sorted(clist): md += '\n---\n\n' diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index 9442de7a..58589156 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -36,6 +36,7 @@ import datetime import enum import re import abc +from typing import Union def _cbrt(x): if x < 0.0: @@ -728,7 +729,7 @@ class Time: such as the orbits of planets around the Sun, or the Moon around the Earth. Historically, Terrestrial Time has also been known by the term *Ephemeris Time* (ET). """ - def __init__(self, ut, tt = None): + def __init__(self, ut : Union[float, str], tt = None): if isinstance(ut, str): # Undocumented hack, to make repr(time) reversible. other = Time.Parse(ut) diff --git a/source/python/astronomy/astronomy.py b/source/python/astronomy/astronomy.py index 74bdd703..fbd1003d 100644 --- a/source/python/astronomy/astronomy.py +++ b/source/python/astronomy/astronomy.py @@ -36,6 +36,7 @@ import datetime import enum import re import abc +from typing import Union def _cbrt(x): if x < 0.0: @@ -728,7 +729,7 @@ class Time: such as the orbits of planets around the Sun, or the Moon around the Earth. Historically, Terrestrial Time has also been known by the term *Ephemeris Time* (ET). """ - def __init__(self, ut, tt = None): + def __init__(self, ut : Union[float, str], tt = None): if isinstance(ut, str): # Undocumented hack, to make repr(time) reversible. other = Time.Parse(ut)