Skip to content

Commit 7bf2c81

Browse files
authored
Simplify the type hints for the supported encodings (#3614)
1 parent 7b5819c commit 7bf2c81

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

pygmt/encodings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Character encodings supported by GMT.
33
44
Currently, Adobe Symbol, Adobe ZapfDingbats, Adobe ISOLatin1+ and ISO-8859-x (x can be
5-
1-11, 13-16) encodings are supported. Adobe Standard encoding is not supported.
5+
1-11, 13-16) encodings are supported. Adobe Standard+ encoding is not supported.
66
77
The corresponding Unicode characters in each Adobe character encoding are generated from
88
the mapping tables and conversion scripts in the

pygmt/helpers/utils.py

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@
1818
from pygmt.encodings import charset
1919
from pygmt.exceptions import GMTInvalidInput
2020

21+
# Type hints for the list of encodings supported by PyGMT.
22+
Encoding = Literal[
23+
"ascii",
24+
"ISOLatin1+",
25+
"ISO-8859-1",
26+
"ISO-8859-2",
27+
"ISO-8859-3",
28+
"ISO-8859-4",
29+
"ISO-8859-5",
30+
"ISO-8859-6",
31+
"ISO-8859-7",
32+
"ISO-8859-8",
33+
"ISO-8859-9",
34+
"ISO-8859-10",
35+
"ISO-8859-11",
36+
"ISO-8859-13",
37+
"ISO-8859-14",
38+
"ISO-8859-15",
39+
"ISO-8859-16",
40+
]
41+
2142

2243
def _validate_data_input(
2344
data=None, x=None, y=None, z=None, required_z=False, required_data=True, kind=None
@@ -123,27 +144,7 @@ def _validate_data_input(
123144
raise GMTInvalidInput(msg)
124145

125146

126-
def _check_encoding(
127-
argstr: str,
128-
) -> Literal[
129-
"ascii",
130-
"ISOLatin1+",
131-
"ISO-8859-1",
132-
"ISO-8859-2",
133-
"ISO-8859-3",
134-
"ISO-8859-4",
135-
"ISO-8859-5",
136-
"ISO-8859-6",
137-
"ISO-8859-7",
138-
"ISO-8859-8",
139-
"ISO-8859-9",
140-
"ISO-8859-10",
141-
"ISO-8859-11",
142-
"ISO-8859-13",
143-
"ISO-8859-14",
144-
"ISO-8859-15",
145-
"ISO-8859-16",
146-
]:
147+
def _check_encoding(argstr: str) -> Encoding:
147148
"""
148149
Check the charset encoding of a string.
149150
@@ -185,9 +186,7 @@ def _check_encoding(
185186
adobe_chars = set(charset["Symbol"].values()) | set(
186187
charset["ZapfDingbats"].values()
187188
)
188-
for encoding in ["ISOLatin1+"] + [f"ISO-8859-{i}" for i in range(1, 17)]:
189-
if encoding == "ISO-8859-12": # ISO-8859-12 was abandoned. Skip it.
190-
continue
189+
for encoding in ["ISOLatin1+"] + [f"ISO-8859-{i}" for i in range(1, 17) if i != 12]:
191190
if all(c in (set(charset[encoding].values()) | adobe_chars) for c in argstr):
192191
return encoding # type: ignore[return-value]
193192
# Return the "ISOLatin1+" encoding if the string contains characters from multiple
@@ -340,34 +339,13 @@ def data_kind(
340339
return kind # type: ignore[return-value]
341340

342341

343-
def non_ascii_to_octal(
344-
argstr: str,
345-
encoding: Literal[
346-
"ascii",
347-
"ISOLatin1+",
348-
"ISO-8859-1",
349-
"ISO-8859-2",
350-
"ISO-8859-3",
351-
"ISO-8859-4",
352-
"ISO-8859-5",
353-
"ISO-8859-6",
354-
"ISO-8859-7",
355-
"ISO-8859-8",
356-
"ISO-8859-9",
357-
"ISO-8859-10",
358-
"ISO-8859-11",
359-
"ISO-8859-13",
360-
"ISO-8859-14",
361-
"ISO-8859-15",
362-
"ISO-8859-16",
363-
] = "ISOLatin1+",
364-
) -> str:
342+
def non_ascii_to_octal(argstr: str, encoding: Encoding = "ISOLatin1+") -> str:
365343
r"""
366344
Translate non-ASCII characters to their corresponding octal codes.
367345
368346
Currently, only non-ASCII characters in the Adobe ISOLatin1+, Adobe Symbol, Adobe
369347
ZapfDingbats, and ISO-8850-x (x can be in 1-11, 13-17) encodings are supported.
370-
The Adobe Standard encoding is not supported yet.
348+
The Adobe Standard+ encoding is not supported.
371349
372350
Parameters
373351
----------

0 commit comments

Comments
 (0)