Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grass.script.utils: Add type hints to some functions #326

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions python/grass/script/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
.. sectionauthor:: Anna Petrasova <kratochanna gmail.com>
"""

from __future__ import annotations

import os
import shutil
import locale
Expand All @@ -29,9 +31,13 @@
import string

from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from _typeshed import FileDescriptorOrPath, StrPath, StrOrBytesPath


def float_or_dms(s):
def float_or_dms(s) -> float:
"""Convert DMS to float.

>>> round(float_or_dms('26:45:30'), 5)
Expand All @@ -48,7 +54,7 @@ def float_or_dms(s):
return sum(float(x) / 60**n for (n, x) in enumerate(s.split(":")))


def separator(sep):
def separator(sep: str) -> str:
"""Returns separator from G_OPT_F_SEP appropriately converted
to character.

Expand Down Expand Up @@ -80,7 +86,9 @@ def separator(sep):
return sep


def diff_files(filename_a, filename_b):
def diff_files(
filename_a: FileDescriptorOrPath, filename_b: FileDescriptorOrPath
) -> list[str]:
"""Diffs two text files and returns difference.

:param str filename_a: first file path
Expand All @@ -96,7 +104,7 @@ def diff_files(filename_a, filename_b):
return list(differ.compare(fh_a.readlines(), fh_b.readlines()))


def try_remove(path):
def try_remove(path: StrOrBytesPath) -> None:
"""Attempt to remove a file; no exception is generated if the
attempt fails.

Expand All @@ -108,7 +116,7 @@ def try_remove(path):
pass


def try_rmdir(path):
def try_rmdir(path: StrOrBytesPath) -> None:
"""Attempt to remove a directory; no exception is generated if the
attempt fails.

Expand All @@ -120,17 +128,17 @@ def try_rmdir(path):
shutil.rmtree(path, ignore_errors=True)


def basename(path, ext=None):
def basename(path: StrPath, ext: str | None = None) -> str:
"""Remove leading directory components and an optional extension
from the specified path

:param str path: path
:param str ext: extension
"""
name = os.path.basename(path)
name: str = os.path.basename(path)
if not ext:
return name
fs = name.rsplit(".", 1)
fs: list[str] = name.rsplit(".", 1)
if len(fs) > 1 and fs[1].lower() == ext:
name = fs[0]
return name
Expand Down Expand Up @@ -354,8 +362,8 @@ def alphanum_key(actual_key):

def get_lib_path(modname, libname=None):
"""Return the path of the libname contained in the module."""
from os.path import isdir, join, sep
from os import getenv
from os.path import isdir, join, sep

if isdir(join(getenv("GISBASE"), "etc", modname)):
path = join(os.getenv("GISBASE"), "etc", modname)
Expand Down
Loading