Skip to content

Commit 1fddf46

Browse files
mhvkeerovaher
authored andcommitted
Backport PR astropy#18329: Avoid stringified type annotations in units
The backport is required to prevent/solve problems analogous to those reported in astropy#18221.
1 parent a27a14b commit 1fddf46

15 files changed

Lines changed: 1495 additions & 1534 deletions

File tree

‎astropy/units/core.py‎

Lines changed: 1351 additions & 1352 deletions
Large diffs are not rendered by default.

‎astropy/units/format/base.py‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from __future__ import annotations
4-
53
import warnings
64
from collections.abc import Iterable
7-
from typing import TYPE_CHECKING, ClassVar, Literal
5+
from typing import ClassVar, Literal
86

97
import numpy as np
108

11-
from astropy.units.core import CompositeUnit, NamedUnit, Unit, get_current_unit_registry
9+
from astropy.extern.ply.lex import LexToken
10+
from astropy.units.core import (
11+
CompositeUnit,
12+
NamedUnit,
13+
Unit,
14+
UnitBase,
15+
get_current_unit_registry,
16+
)
1217
from astropy.units.errors import UnitsWarning
18+
from astropy.units.typing import UnitPower, UnitScale
1319
from astropy.units.utils import maybe_simple_fraction
1420
from astropy.utils.misc import did_you_mean
1521

16-
if TYPE_CHECKING:
17-
from astropy.extern.ply.lex import LexToken
18-
from astropy.units import UnitBase
19-
from astropy.units.typing import UnitPower, UnitScale
20-
2122

2223
class Base:
2324
"""
2425
The abstract base class of all unit formats.
2526
"""
2627

27-
registry: ClassVar[dict[str, type[Base]]] = {}
28+
registry: ClassVar[dict[str, type["Base"]]] = {}
2829
_space: ClassVar[str] = " "
2930
_scale_unit_separator: ClassVar[str] = " "
3031
_times: ClassVar[str] = "*"

‎astropy/units/format/cds.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212

1313
"""Handles the CDS string format for units."""
1414

15-
from __future__ import annotations
16-
1715
import re
18-
from typing import TYPE_CHECKING, ClassVar, Literal
16+
from typing import ClassVar, Literal
1917

20-
from astropy.units.core import CompositeUnit, Unit
18+
from astropy.extern.ply.lex import Lexer
19+
from astropy.units.core import CompositeUnit, Unit, UnitBase
2120
from astropy.units.utils import is_effectively_unity
2221
from astropy.utils import classproperty, parsing
22+
from astropy.utils.parsing import ThreadSafeParser
2323

2424
from .base import Base, _ParsingFormatMixin
2525

26-
if TYPE_CHECKING:
27-
from astropy.extern.ply.lex import Lexer
28-
from astropy.units import UnitBase
29-
from astropy.utils.parsing import ThreadSafeParser
30-
3126

3227
class CDS(Base, _ParsingFormatMixin):
3328
"""

‎astropy/units/format/console.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
Handles the "Console" unit format.
55
"""
66

7-
from __future__ import annotations
7+
from typing import ClassVar, Literal
88

9-
from typing import TYPE_CHECKING, ClassVar, Literal
9+
from astropy.units.core import UnitBase
1010

1111
from . import base
1212

13-
if TYPE_CHECKING:
14-
from astropy.units import UnitBase
15-
1613

1714
class Console(base.Base):
1815
"""

‎astropy/units/format/fits.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44
Handles the "FITS" unit format.
55
"""
66

7-
from __future__ import annotations
8-
9-
from typing import TYPE_CHECKING, Literal
7+
from typing import Literal
108

119
import numpy as np
1210

13-
from astropy.units.core import CompositeUnit
11+
from astropy.units.core import CompositeUnit, UnitBase
1412
from astropy.units.errors import UnitScaleError
1513
from astropy.utils import classproperty
1614

1715
from . import Base, utils
1816
from .generic import _GenericParserMixin
1917

20-
if TYPE_CHECKING:
21-
from astropy.units import UnitBase
22-
2318

2419
class FITS(Base, _GenericParserMixin):
2520
"""

‎astropy/units/format/generic.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,25 @@
1414
Handles a "generic" string format for units
1515
"""
1616

17-
from __future__ import annotations
18-
1917
import re
2018
import unicodedata
2119
import warnings
2220
from fractions import Fraction
2321
from re import Match, Pattern
24-
from typing import TYPE_CHECKING, ClassVar, Final
22+
from typing import ClassVar, Final
2523

2624
import numpy as np
2725

28-
from astropy.units.core import CompositeUnit, Unit, get_current_unit_registry
26+
from astropy.extern.ply.lex import Lexer
27+
from astropy.units.core import CompositeUnit, Unit, UnitBase, get_current_unit_registry
2928
from astropy.units.errors import UnitsWarning
29+
from astropy.units.typing import UnitScale
3030
from astropy.utils import classproperty, parsing
3131
from astropy.utils.misc import did_you_mean
32+
from astropy.utils.parsing import ThreadSafeParser
3233

3334
from .base import Base, _ParsingFormatMixin
3435

35-
if TYPE_CHECKING:
36-
from astropy.extern.ply.lex import Lexer
37-
from astropy.units import UnitBase
38-
from astropy.units.typing import UnitScale
39-
from astropy.utils.parsing import ThreadSafeParser
40-
4136

4237
class _GenericParserMixin(_ParsingFormatMixin):
4338
"""Provide the parser used by Generic, FITS and VOUnit."""

‎astropy/units/format/latex.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
Handles the "LaTeX" unit format.
55
"""
66

7-
from __future__ import annotations
8-
97
import re
10-
from typing import TYPE_CHECKING, ClassVar, Literal
8+
from typing import ClassVar, Literal
119

12-
from . import console
10+
from astropy.units.core import NamedUnit, UnitBase
11+
from astropy.units.typing import UnitPower
1312

14-
if TYPE_CHECKING:
15-
from astropy.units import NamedUnit, UnitBase
16-
from astropy.units.typing import UnitPower
13+
from . import console
1714

1815

1916
class Latex(console.Console):

‎astropy/units/format/ogip.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,23 @@
1616
<https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/>`__.
1717
"""
1818

19-
from __future__ import annotations
20-
2119
import math
2220
import warnings
2321
from fractions import Fraction
24-
from typing import TYPE_CHECKING, ClassVar, Literal
22+
from typing import ClassVar, Literal
2523

2624
import numpy as np
2725

28-
from astropy.units.core import CompositeUnit
26+
from astropy.extern.ply.lex import Lexer
27+
from astropy.units.core import CompositeUnit, UnitBase
2928
from astropy.units.errors import UnitParserWarning, UnitsWarning
29+
from astropy.units.typing import UnitScale
3030
from astropy.utils import classproperty, parsing
31+
from astropy.utils.parsing import ThreadSafeParser
3132

3233
from . import utils
3334
from .base import Base, _ParsingFormatMixin
3435

35-
if TYPE_CHECKING:
36-
from astropy.extern.ply.lex import Lexer
37-
from astropy.units import UnitBase
38-
from astropy.units.typing import UnitScale
39-
from astropy.utils.parsing import ThreadSafeParser
40-
4136

4237
class OGIP(Base, _ParsingFormatMixin):
4338
"""

‎astropy/units/format/unicode_format.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
Handles the "Unicode" unit format.
55
"""
66

7-
from __future__ import annotations
7+
from typing import ClassVar
88

9-
from typing import TYPE_CHECKING, ClassVar
9+
from astropy.units.core import NamedUnit
10+
from astropy.units.typing import UnitPower
1011

1112
from . import console
1213

13-
if TYPE_CHECKING:
14-
from astropy.units import NamedUnit
15-
from astropy.units.typing import UnitPower
16-
1714

1815
class Unicode(console.Console):
1916
"""

‎astropy/units/format/vounit.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
Handles the "VOUnit" unit format.
44
"""
55

6-
from __future__ import annotations
7-
86
import re
97
import warnings
108
from re import Pattern
11-
from typing import TYPE_CHECKING, ClassVar, Literal
9+
from typing import ClassVar, Literal
1210

1311
import numpy as np
1412

13+
from astropy.extern.ply.lex import LexToken
1514
from astropy.units.core import (
1615
CompositeUnit,
1716
NamedUnit,
1817
PrefixUnit,
18+
UnitBase,
1919
def_unit,
2020
dimensionless_unscaled,
2121
si_prefixes,
@@ -26,16 +26,12 @@
2626
UnitsError,
2727
UnitsWarning,
2828
)
29+
from astropy.units.typing import UnitScale
2930
from astropy.utils import classproperty
3031

3132
from . import Base, utils
3233
from .generic import _GenericParserMixin
3334

34-
if TYPE_CHECKING:
35-
from astropy.extern.ply.lex import LexToken
36-
from astropy.units import UnitBase
37-
from astropy.units.typing import UnitScale
38-
3935

4036
class VOUnit(Base, _GenericParserMixin):
4137
"""

0 commit comments

Comments
 (0)