From 2a89ba0751f1f3a93a78caab1050baabcb65ab1d Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Sat, 14 Oct 2023 02:00:07 +0200 Subject: [PATCH] Various type fixes and tweaks --- moderngl_window/__init__.py | 7 ++-- moderngl_window/context/base/window.py | 6 ++-- moderngl_window/loaders/scene/gltf2.py | 2 +- moderngl_window/loaders/texture/cube.py | 2 +- moderngl_window/meta/program.py | 22 ++++++------ moderngl_window/opengl/program.py | 45 ++++++++++++----------- moderngl_window/resources/__init__.py | 2 +- pyproject.toml | 47 +++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 44 deletions(-) diff --git a/moderngl_window/__init__.py b/moderngl_window/__init__.py index 0cdf3675..55fcf443 100644 --- a/moderngl_window/__init__.py +++ b/moderngl_window/__init__.py @@ -9,7 +9,7 @@ import weakref from pathlib import Path -from typing import List, Type +from typing import List, Type, Optional import moderngl from moderngl_window.context.base import WindowConfig, BaseWindow @@ -60,9 +60,8 @@ def setup_basic_logging(level: int): class ContextRefs: """Namespace for window/context references""" - - WINDOW = None - CONTEXT = None + WINDOW: Optional[BaseWindow] = None + CONTEXT: Optional[moderngl.Context] = None def activate_context(window: BaseWindow = None, ctx: moderngl.Context = None): diff --git a/moderngl_window/context/base/window.py b/moderngl_window/context/base/window.py index 752c42ce..cf158a97 100644 --- a/moderngl_window/context/base/window.py +++ b/moderngl_window/context/base/window.py @@ -4,7 +4,7 @@ import logging import sys import weakref -from typing import Any, Tuple, Type, List, Optional +from typing import Any, Tuple, Type, List, Optional, Dict import moderngl from moderngl_window.context.base import KeyModifiers, BaseKeys @@ -1374,7 +1374,7 @@ def load_program( tess_control_shader=None, tess_evaluation_shader=None, defines: dict = None, - varyings: List[str] = None, + varyings: Optional[List[str]] = None, ) -> moderngl.Program: """Loads a shader program. @@ -1412,7 +1412,7 @@ def load_program( ) def load_compute_shader( - self, path, defines: dict = None, **kwargs + self, path, defines: Optional[Dict] = None, **kwargs ) -> moderngl.ComputeShader: """Loads a compute shader. diff --git a/moderngl_window/loaders/scene/gltf2.py b/moderngl_window/loaders/scene/gltf2.py index d1131ed2..8dc65494 100644 --- a/moderngl_window/loaders/scene/gltf2.py +++ b/moderngl_window/loaders/scene/gltf2.py @@ -152,7 +152,7 @@ def load_glb(self): version = struct.unpack(" moderngl.Context: @@ -209,8 +209,8 @@ class ShaderSource: """ Helper class representing a single shader type. - It ensures the source has the right format, injects ``#define`` preprocessors, - resolves ``#include`` preprocessors etc. + It ensures the source has the right format, injects ``#define`` pre-processors, + resolves ``#include`` pre-processors etc. A ``ShaderSource`` can be the base/root shader or a source referenced in an ``#include``. """ @@ -250,16 +250,14 @@ def __init__( if self._root and not self._lines[0].startswith("#version"): self.print() raise ShaderError( - "Missing #version in {}. A version must be defined in the first line".format( - self._name - ), + f"Missing #version in {self._name}. A version must be defined in the first line" ) self.apply_defines(defines) # Inject source with shade type if self._root: - self._lines.insert(1, "#define {} 1".format(self._type)) + self._lines.insert(1, f"#define {self._type} 1") self._lines.insert(2, "#line 2") @property @@ -299,12 +297,13 @@ def defines(self) -> dict: def handle_includes(self, load_source_func, depth=0, source_id=0): """Inject includes into the shader source. - This happens recursively up to a max level in case the users has circular includes. - We also build up a list of all the included sources in the root shader. + This happens recursively up to a max level in case the users has + circular includes. We also build up a list of all the included + sources in the root shader. Args: load_source_func (func): A function for finding and loading a source - depth (int): The current include depth (incease by 1 for every call) + depth (int): The current include depth (increase by 1 for every call) """ if depth > 100: raise ShaderError( @@ -351,7 +350,7 @@ def apply_defines(self, defines: dict): if not value: continue - self.lines[nr] = "#define {} {}".format(name, str(value)) + self.lines[nr] = f"#define {name} {value}" except IndexError: pass @@ -374,19 +373,19 @@ def find_out_attribs(self) -> List[str]: def print(self): """Print the shader lines (for debugging)""" - print("---[ START {} ]---".format(self.name)) + print(f"---[ START {self.name} ]---") for i, line in enumerate(self.lines): - print("{}: {}".format(str(i).zfill(3), line)) + print(f"{str(i).zfill(3)}: {line}") - print("---[ END {} ]---".format(self.name)) + print("---[ END {self.name} ]---") def __repr__(self): - return "".format(self.name, self.id) + return f"" class ShaderError(Exception): - pass + """Generic shader related error""" class ReloadableProgram: @@ -481,4 +480,4 @@ def geometry_vertices(self) -> int: return self.program.geometry_vertices def __repr__(self): - return "".format(self.name, self.glo) + return f"" diff --git a/moderngl_window/resources/__init__.py b/moderngl_window/resources/__init__.py index 304484d5..1aa99a35 100644 --- a/moderngl_window/resources/__init__.py +++ b/moderngl_window/resources/__init__.py @@ -80,7 +80,7 @@ def _append_unique_path(path: Union[Path, str], dest: list): @contextmanager -def temporary_dirs(dirs: Union[Path, str] = []): +def temporary_dirs(dirs: Union[Path, str]): """Temporarily changes all resource directories Example:: diff --git a/pyproject.toml b/pyproject.toml index c1a1b696..9e222eb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,3 +84,50 @@ exclude = [ "docs/", "temp/", ] + +[tool.mypy] +disable_error_code = "annotation-unchecked" + +[[tool.mypy.overrides]] +module = "pyglet.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "PIL.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "rocket.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "imgui.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "sdl2.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "pyopengltk.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "PySide2.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "pygame.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "glfw.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "PyQt5.*" +ignore_missing_imports = true + +[[tool.mypy.overrides]] +module = "pyrr.*" +ignore_missing_imports = true