From 5f2b1a432e7725cab0310a7d7af93ec6c11db35d Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Thu, 1 Aug 2024 00:21:55 -0700 Subject: [PATCH] improve type annotations --- mem_edit/abstract.py | 6 +++--- mem_edit/utils.py | 13 ++++++------- mem_edit/windows.py | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/mem_edit/abstract.py b/mem_edit/abstract.py index 8e1e2f3..f21a528 100644 --- a/mem_edit/abstract.py +++ b/mem_edit/abstract.py @@ -1,8 +1,8 @@ """ Abstract class for cross-platform memory editing. """ - -from typing import Generator +from typing import Self +from collections.abc import Generator from abc import ABCMeta, abstractmethod from contextlib import contextmanager import copy @@ -346,7 +346,7 @@ class Process(metaclass=ABCMeta): @classmethod @contextmanager - def open_process(cls, process_id: int) -> Generator['Process', None, None]: + def open_process(cls: type[Self], process_id: int) -> Generator[Self, None, None]: """ Context manager which automatically closes the constructed Process: ``` diff --git a/mem_edit/utils.py b/mem_edit/utils.py index 5099845..fac9f3c 100644 --- a/mem_edit/utils.py +++ b/mem_edit/utils.py @@ -11,16 +11,15 @@ Utility functions and types: Check if two buffers (ctypes objects) store equal values: ctypes_equal(a, b) """ -from typing import Union import ctypes -ctypes_buffer_t = Union[ - ctypes._SimpleCData, - ctypes.Array, - ctypes.Structure, - ctypes.Union, - ] +ctypes_buffer_t = ( + ctypes._SimpleCData + | ctypes.Array + | ctypes.Structure + | ctypes.Union + ) class MemEditError(Exception): diff --git a/mem_edit/windows.py b/mem_edit/windows.py index 9de6b06..c165f99 100644 --- a/mem_edit/windows.py +++ b/mem_edit/windows.py @@ -92,7 +92,7 @@ class MEMORY_BASIC_INFORMATION64(ctypes.Structure): PTR_SIZE = ctypes.sizeof(ctypes.c_void_p) -MEMORY_BASIC_INFORMATION: ctypes.Structure +MEMORY_BASIC_INFORMATION: type[ctypes.Structure] if PTR_SIZE == 8: # 64-bit python MEMORY_BASIC_INFORMATION = MEMORY_BASIC_INFORMATION64 elif PTR_SIZE == 4: # 32-bit python @@ -256,7 +256,7 @@ class Process(AbstractProcess): start = sys_info.lpMinimumApplicationAddress stop = sys_info.lpMaximumApplicationAddress - def get_mem_info(address): + def get_mem_info(address: int) -> MEMORY_BASIC_INFORMATION: """ Query the memory region starting at or before 'address' to get its size/type/state/permissions. """