From 3030fb9e30d7724c05ea82e1ef6a52e35de45b19 Mon Sep 17 00:00:00 2001 From: JB Lovland Date: Mon, 4 Dec 2023 14:25:30 +0100 Subject: [PATCH] CLN: Add types grid-import --- mypy.ini | 3 -- src/xtgeo/grid3d/_grid_import.py | 55 ++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/mypy.ini b/mypy.ini index 3f5779acd..d5aada172 100644 --- a/mypy.ini +++ b/mypy.ini @@ -60,9 +60,6 @@ ignore_errors = True [mypy-xtgeo.grid3d._grid_export] ignore_errors = True -[mypy-xtgeo.grid3d._grid_import] -ignore_errors = True - [mypy-xtgeo.grid3d._grid_import_ecl] ignore_errors = True diff --git a/src/xtgeo/grid3d/_grid_import.py b/src/xtgeo/grid3d/_grid_import.py index ee824086e..75b26cf97 100644 --- a/src/xtgeo/grid3d/_grid_import.py +++ b/src/xtgeo/grid3d/_grid_import.py @@ -1,7 +1,11 @@ """Grid import functions for various formats.""" +from __future__ import annotations + +from pathlib import Path +from typing import Any, Literal -import xtgeo from xtgeo.common import null_logger +from xtgeo.common.sys import _XTGeoFile from xtgeo.grid3d import _grid_import_ecl, _grid_import_roff from . import _grid_import_xtgcpgeom @@ -9,46 +13,65 @@ logger = null_logger(__name__) -def from_file(gfile, fformat=None, **kwargs): +def from_file( + gfile: _XTGeoFile, + fformat: Literal[ + "bgrdecl", + "egrid", + "fegrid", + "grdecl", + "guess", + "hdf", + "roff_ascii", + "roff_binary", + "xtg", + ] + | None = None, + **kwargs: Any, +) -> dict[str, Any]: """Import grid geometry from file, and makes an instance of this class. Returns: dictionary of keyword arguments to be used in Grid constructor. """ - if not isinstance(gfile, xtgeo._XTGeoFile): + if not isinstance(gfile, _XTGeoFile): raise RuntimeError("Error gfile must be a _XTGeoFile instance") - result = {} + result: dict[str, Any] = dict() result["filesrc"] = gfile.name - if fformat is None or fformat == "guess": - fformat = gfile.detect_fformat() - else: - fformat = gfile.generic_format_by_proposal(fformat) # default + _fformat = ( + gfile.detect_fformat() + if fformat is None or fformat == "guess" + else gfile.generic_format_by_proposal(fformat) # default + ) gfile.check_file(raiseerror=IOError, raisetext=f"Cannot access file {gfile.name}") - if fformat in ["roff_binary", "roff_ascii"]: + if _fformat in ["roff_binary", "roff_ascii"]: result.update(_grid_import_roff.import_roff(gfile, **kwargs)) - elif fformat in ["egrid", "fegrid"]: + elif _fformat in ["egrid", "fegrid"]: result.update( - _grid_import_ecl.import_ecl_egrid(gfile, fileformat=fformat, **kwargs) + _grid_import_ecl.import_ecl_egrid(gfile, fileformat=_fformat, **kwargs) ) - elif fformat == "grdecl": + elif _fformat == "grdecl": result.update(_grid_import_ecl.import_ecl_grdecl(gfile, **kwargs)) - elif fformat == "bgrdecl": + elif _fformat == "bgrdecl": result.update(_grid_import_ecl.import_ecl_bgrdecl(gfile, **kwargs)) - elif fformat == "xtg": + elif _fformat == "xtg": result.update(_grid_import_xtgcpgeom.import_xtgcpgeom(gfile, **kwargs)) - elif fformat == "hdf": + elif _fformat == "hdf": result.update(_grid_import_xtgcpgeom.import_hdf5_cpgeom(gfile, **kwargs)) else: - raise ValueError(f"Invalid file format: {fformat}") + raise ValueError(f"Invalid file format: {_fformat}") if gfile.memstream: result["name"] = "unknown" else: + # Mypy does not know that if gfile.memstream -> False + # then .file must be Path. + assert isinstance(gfile.file, Path) result["name"] = gfile.file.stem return result