#13982 Python: Tighten Instance.launch/find return type and drop dead None checks

Instance.launch and Instance.find now always either return a connected
Instance or raise RipsError, so their return type narrows from
Optional[Instance] to Instance. In launch(), the unreachable `if pid:`
guard around the Popen result and the trailing `return None` are
removed; the only failure paths raise RipsError.

Update the PythonExamples that previously branched on a None return:
- Drop `if resinsight is None: exit(1)` style guards and `if resinsight
  is not None:` wrappers; dedent the body where applicable.
- export_corner_point_grid.py used the None check as a fallback to
  launch ResInsight; rewrite as try/except rips.RipsError so the
  fallback still works.
- instance_example.py is repurposed as a try/except example for
  reporting connection failure.
This commit is contained in:
Kristian Bendiksen committed 2026-05-13 13:47:40 +02:00
1 parent 1424a6dc53
commit e7608536cf
18 files changed
+262 -285

No files matched your search

+13 -19
View File
@@ -116,7 +116,7 @@ class Instance:
init_timeout: int = 300,
command_line_parameters: List[str] = [],
enable_heartbeat: bool = True,
) -> Optional[Instance]:
) -> Instance:
"""Launch a new Instance of ResInsight. This requires the environment variable
RESINSIGHT_EXECUTABLE to be set or the parameter resinsight_executable to be provided.
The RESINSIGHT_GRPC_PORT environment variable can be set to an alternative port number.
@@ -135,7 +135,7 @@ class Instance:
server periodically and aborts pending RPCs if it dies. Disable on
slow boxes where false positives matter (long GC pauses, debugger).
Returns:
Instance: an instance object if it worked. None if not.
Instance: a connected instance object. Raises :class:`RipsError` on failure.
"""
requested_port: int = 50051
@@ -193,29 +193,23 @@ class Instance:
process = subprocess.Popen(parameters)
pid = process.pid
if pid:
port = Instance.__read_port_number_from_file(
port_number_file, init_timeout
)
if port == -1:
# Need to kill the process using PID since there is no GRPC connection to use.
Instance.__kill_process(pid)
raise RipsError("Unable to read port number. Launch failed.")
else:
instance = Instance(
port=port,
launched=True,
enable_heartbeat=enable_heartbeat,
)
return instance
return None
port = Instance.__read_port_number_from_file(port_number_file, init_timeout)
if port == -1:
# Need to kill the process using PID since there is no GRPC connection to use.
Instance.__kill_process(pid)
raise RipsError("Unable to read port number. Launch failed.")
return Instance(
port=port,
launched=True,
enable_heartbeat=enable_heartbeat,
)
@staticmethod
def find(
start_port: int = 50051,
end_port: int = 50071,
enable_heartbeat: bool = True,
) -> Optional[Instance]:
) -> Instance:
"""Search for an existing Instance of ResInsight by testing ports.
By default we search from port 50051 to 50071 or if the environment