Skip to content

Commit

Permalink
Using fully type-qualified os.PathLike.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 672606343
  • Loading branch information
agutkin authored and copybara-github committed Sep 9, 2024
1 parent c18911c commit bc030d9
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 41 deletions.
20 changes: 10 additions & 10 deletions nisaba/scripts/brahmic/iso.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@


def brahmic_to_iso(
consonant_file: os.PathLike,
inherent_vowel_file: os.PathLike,
vowel_sign_file: os.PathLike,
vowel_file: os.PathLike,
vowel_length_sign_file: os.PathLike,
coda_file: os.PathLike,
dead_consonant_file: os.PathLike,
standalone_file: os.PathLike,
subjoined_consonant_file: os.PathLike,
virama_file: os.PathLike,
consonant_file: os.PathLike[str],
inherent_vowel_file: os.PathLike[str],
vowel_sign_file: os.PathLike[str],
vowel_file: os.PathLike[str],
vowel_length_sign_file: os.PathLike[str],
coda_file: os.PathLike[str],
dead_consonant_file: os.PathLike[str],
standalone_file: os.PathLike[str],
subjoined_consonant_file: os.PathLike[str],
virama_file: os.PathLike[str],
) -> p.Fst:
"""Creates an FST that transduces a Brahmic script to ISO 15919.
Expand Down
2 changes: 1 addition & 1 deletion nisaba/scripts/brahmic/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def OpenSigma(script: str, token_type: str) -> pynini.Fst:


def MaybeLoadScriptConfig(
file_path: os.PathLike) -> script_config_pb2.ScriptConfig:
file_path: os.PathLike[str]) -> script_config_pb2.ScriptConfig:
"""Loads script configuration, if present."""
pb = script_config_pb2.ScriptConfig()
if not uf.IsFileExist(file_path):
Expand Down
6 changes: 3 additions & 3 deletions nisaba/scripts/brahmic/visual_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
'Token type: utf8 or byte')


def core_visual_norm_fsts(rewrite_file: os.PathLike,
preserve_file: os.PathLike,
consonant_file: os.PathLike,
def core_visual_norm_fsts(rewrite_file: os.PathLike[str],
preserve_file: os.PathLike[str],
consonant_file: os.PathLike[str],
sigma: pynini.Fst) -> List[pynini.Fst]:
"""Creates a visual normalization FST.
Expand Down
26 changes: 13 additions & 13 deletions nisaba/scripts/brahmic/wellformed.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@
import nisaba.scripts.utils.file as uf


def _input_string_file(filename: os.PathLike,
def _input_string_file(filename: os.PathLike[str],
return_if_empty: pynini.Fst = uf.EMPTY) -> pynini.Fst:
fst = uf.StringFile(filename, return_if_empty)
return pynini.project(fst, 'input').rmepsilon()


def accept_well_formed(script_config_file: os.PathLike,
consonant_file: os.PathLike,
dead_consonant_file: os.PathLike,
subjoined_consonant_file: os.PathLike,
vowel_sign_file: os.PathLike,
vowel_file: os.PathLike,
vowel_length_sign_file: os.PathLike,
coda_file: os.PathLike,
standalone_file: os.PathLike,
virama_file: os.PathLike,
accept_file: os.PathLike,
preserve_file: os.PathLike) -> pynini.Fst:
def accept_well_formed(script_config_file: os.PathLike[str],
consonant_file: os.PathLike[str],
dead_consonant_file: os.PathLike[str],
subjoined_consonant_file: os.PathLike[str],
vowel_sign_file: os.PathLike[str],
vowel_file: os.PathLike[str],
vowel_length_sign_file: os.PathLike[str],
coda_file: os.PathLike[str],
standalone_file: os.PathLike[str],
virama_file: os.PathLike[str],
accept_file: os.PathLike[str],
preserve_file: os.PathLike[str]) -> pynini.Fst:
"""Create an unweighted FSA to accept the well-formed strings in a script.
Args:
Expand Down
11 changes: 7 additions & 4 deletions nisaba/scripts/utils/char.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
ZWS = "\u200B" # Zero Width Space


def _read_string_file_chars_to_set(files: Iterable[os.PathLike],
relevant_fields: int) -> Set[str]:
def _read_string_file_chars_to_set(
files: Iterable[os.PathLike[str]], relevant_fields: int
) -> Set[str]:
"""Reads the characters under some selection from some file paths into a set.
Arguments:
Expand All @@ -51,8 +52,10 @@ def _read_string_file_chars_to_set(files: Iterable[os.PathLike],
return chars


def derive_chars(both_sides: Iterable[os.PathLike] = (),
input_side: Iterable[os.PathLike] = ()) -> Set[str]:
def derive_chars(
both_sides: Iterable[os.PathLike[str]] = (),
input_side: Iterable[os.PathLike[str]] = ()
) -> Set[str]:
"""Create the set of characters in a script from StringFiles.
Args:
Expand Down
6 changes: 3 additions & 3 deletions nisaba/scripts/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
EPSILON: pynini.Fst = pynini.accep("").optimize()


def AsResourcePath(filename: os.PathLike) -> os.PathLike:
def AsResourcePath(filename: os.PathLike[str]) -> os.PathLike[str]:
filename = os.fspath(filename)
return pathlib.Path(runfiles.Create().Rlocation(filename))


def IsFileExist(filename: os.PathLike) -> bool:
def IsFileExist(filename: os.PathLike[str]) -> bool:
"""Checks if a resource file exists."""
try:
if os.path.isfile(AsResourcePath(filename)):
Expand All @@ -47,7 +47,7 @@ def OnEmpty(fst, return_if_empty=EMPTY):
return return_if_empty if fst.start() == pynini.NO_STATE_ID else fst


def StringFile(filename: os.PathLike,
def StringFile(filename: os.PathLike[str],
return_if_empty: pynini.Fst = EMPTY) -> pynini.Fst:
"""Reads FST from `filename`. If FST is empty returns `return_if_empty`."""
return OnEmpty(pynini.string_file(AsResourcePath(filename)), return_if_empty)
Expand Down
4 changes: 3 additions & 1 deletion nisaba/scripts/utils/letter_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def _fill_missing_raw(pb: ll.LetterLanguages) -> None:
item.letter.raw, _ = us.convert_item(pb.uname_prefix, [], item.letter)


def read_textproto(proto_path: Union[str, os.PathLike]) -> ll.LetterLanguages:
def read_textproto(
proto_path: Union[str, os.PathLike[str]]
) -> ll.LetterLanguages:
pb = proto.read_textproto(proto_path, ll.LetterLanguages())
_fill_missing_raw(pb)
logging.info('Read %d letters.', len(pb.item))
Expand Down
2 changes: 1 addition & 1 deletion nisaba/scripts/utils/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


def read_textproto(
proto_path: Union[str, os.PathLike], proto: _ParsableT
proto_path: Union[str, os.PathLike[str]], proto: _ParsableT
) -> _ParsableT:
logging.info('Parsing %s ...', proto_path)
if not os.path.exists(proto_path):
Expand Down
10 changes: 6 additions & 4 deletions nisaba/scripts/utils/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
RuleSets = List[RuleSet]


def rules_from_string_file(file: os.PathLike) -> Iterator[Rule]:
def rules_from_string_file(file: os.PathLike[str]) -> Iterator[Rule]:
"""Yields string rules from a text resource with unweighted string maps."""
return rules_from_string_path(uf.AsResourcePath(file))


def rules_from_string_path(file: os.PathLike) -> Iterator[Rule]:
def rules_from_string_path(file: os.PathLike[str]) -> Iterator[Rule]:
"""Yields string rules from a text file with unweighted string maps."""
with pathlib.Path(file).open('rt') as f:
df = pd.read_csv(f, sep='\t', comment='#', escapechar='\\',
Expand Down Expand Up @@ -143,7 +143,9 @@ def fst_from_rules(rules: RuleSet, sigma: pynini.Fst) -> pynini.Fst:
return ur.RewriteAndComposeFsts(fsts, sigma)


def fst_from_rule_file(rule_file: os.PathLike, sigma: pynini.Fst) -> pynini.Fst:
def fst_from_rule_file(
rule_file: os.PathLike[str], sigma: pynini.Fst
) -> pynini.Fst:
"""Gets rewrite FST from a given rewrite rule file.
Args:
Expand Down Expand Up @@ -172,7 +174,7 @@ def _fst_from_cascading_rules(rules: RuleSet, sigma: pynini.Fst) -> pynini.Fst:
return ur.RewriteAndComposeFsts(fsts, sigma)


def fst_from_cascading_rule_file(rule_file: os.PathLike,
def fst_from_cascading_rule_file(rule_file: os.PathLike[str],
sigma: pynini.Fst) -> pynini.Fst:
"""Gets rewrite FST from a given rewrite rule file.
Expand Down
2 changes: 1 addition & 1 deletion nisaba/scripts/utils/unicode_strings_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _fill_missing_raw(pb: unicode_strings_pb2.UnicodeStrings) -> None:


def read_textproto(
proto_path: Union[str, os.PathLike],
proto_path: Union[str, os.PathLike[str]],
) -> unicode_strings_pb2.UnicodeStrings:
pb = proto.read_textproto(proto_path, unicode_strings_pb2.UnicodeStrings())
_fill_missing_raw(pb)
Expand Down

0 comments on commit bc030d9

Please sign in to comment.