Skip to content

Commit 6f76219

Browse files
committed
more typing fixes
1 parent d4edf4c commit 6f76219

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

arcade/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@ def __init__(
230230
alpha_size=8,
231231
)
232232
try:
233+
# This type ignore is here because somehow Pyright thinks this is an Emscripten window
233234
super().__init__(
234235
width=width,
235236
height=height,
236237
caption=title,
237238
resizable=resizable,
238-
config=config, # type: ignore Pylance seems to think this is an EmscriptenWindow at this point?
239+
config=config, # type: ignore
239240
vsync=vsync,
240241
visible=visible,
241242
style=style,
@@ -356,8 +357,10 @@ def current_view(self) -> View | None:
356357
"""
357358
return self._current_view
358359

360+
# TODO: This is overriding the ctx function from Pyglet's BaseWindow which returns the
361+
# SurfaceContext class from pyglet. We should probably rename this.
359362
@property
360-
def ctx(self) -> ArcadeContext:
363+
def ctx(self) -> ArcadeContext: # type: ignore
361364
"""
362365
The OpenGL context for this window.
363366

arcade/context.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from arcade.camera import Projector
1717
from arcade.camera.default import DefaultProjector
1818
from arcade.gl import BufferDescription, Context
19+
from arcade.gl.buffer import Buffer
1920
from arcade.gl.compute_shader import ComputeShader
2021
from arcade.gl.framebuffer import Framebuffer
2122
from arcade.gl.program import Program
23+
from arcade.gl.query import Query
2224
from arcade.gl.texture import Texture2D
2325
from arcade.gl.vertex_array import Geometry
2426
from arcade.texture_atlas import DefaultTextureAtlas, TextureAtlasBase
@@ -55,7 +57,9 @@ def __init__(
5557
gl_api: str = "gl",
5658
) -> None:
5759
# Set up a default orthogonal projection for sprites and shapes
58-
self._window_block = window._matrices.ubo
60+
# Mypy can't figure out the dynamic creation of the matrices in Pyglet
61+
# They are created based on the active backend.
62+
self._window_block = window._matrices.ubo # type: ignore
5963
self.bind_window_block()
6064

6165
self.blend_func = self.BLEND_DEFAULT
@@ -191,23 +195,26 @@ def __init__(
191195
if gl_api != "webgl":
192196
# SpriteList collision resources
193197
# Buffer version of the collision detection program.
194-
self.collision_detection_program = self.load_program(
198+
self.collision_detection_program: Program | None = self.load_program(
195199
vertex_shader=":system:shaders/collision/col_trans_vs.glsl",
196200
geometry_shader=":system:shaders/collision/col_trans_gs.glsl",
197201
)
198202
# Texture version of the collision detection program.
199-
self.collision_detection_program_simple = self.load_program(
203+
self.collision_detection_program_simple: Program | None = self.load_program(
200204
vertex_shader=":system:shaders/collision/col_tex_trans_vs.glsl",
201205
geometry_shader=":system:shaders/collision/col_tex_trans_gs.glsl",
202206
)
203207
self.collision_detection_program_simple["pos_angle_data"] = 0
204208
self.collision_detection_program_simple["size_data"] = 1
205209
self.collision_detection_program_simple["index_data"] = 2
206210

207-
self.collision_buffer = self.buffer(reserve=1024 * 4)
208-
self.collision_query = self.query(samples=False, time=False, primitives=True)
211+
self.collision_buffer: Buffer | None = self.buffer(reserve=1024 * 4)
212+
self.collision_query: Query | None = self.query(
213+
samples=False, time=False, primitives=True
214+
)
209215
else:
210216
self.collision_detection_program = None
217+
self.collision_detection_program_simple = None
211218
self.collision_buffer = None
212219
self.collision_query = None
213220

arcade/sprite_list/sprite_list.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,25 +1685,30 @@ def get_nearby_sprite_indices(self, pos: Point, size: Point, length: int) -> lis
16851685
A list of indices of nearby sprites.
16861686
"""
16871687
ctx = self.ctx
1688+
if ctx._gl_api == "webgl":
1689+
raise RuntimeError("GPU Collision is not supported on WebGL Backends")
1690+
1691+
# All of these type ignores are because of GPU collision not being supported on WebGL
1692+
# Unfortuantely the type checkers don't have a sane way of understanding that, and it's
1693+
# not worth run-time checking all of these things, because they are guaranteed based on
1694+
# active GL api of the context. Pyright actually does seem to be able to figure it out
1695+
# but mypy does not
16881696

1689-
ctx.collision_detection_program["check_pos"] = pos
1690-
ctx.collision_detection_program["check_size"] = size
1697+
ctx.collision_detection_program["check_pos"] = pos # type: ignore
1698+
ctx.collision_detection_program["check_size"] = size # type: ignore
16911699
buffer = ctx.collision_buffer
1692-
with ctx.collision_query:
1693-
# These calls are typed as | None because of the arcade.gl backends, but at this
1694-
# point if we have SpriteListBufferData these are guaranteed to be on a backend
1695-
# where they will not be None.
1700+
with ctx.collision_query: # type: ignore
16961701
self._geometry.transform(
16971702
ctx.collision_detection_program, # type: ignore
16981703
buffer, # type: ignore
16991704
vertices=length,
17001705
)
17011706

17021707
# Store the number of sprites emitted
1703-
emit_count = ctx.collision_query.primitives_generated
1708+
emit_count = ctx.collision_query.primitives_generated # type: ignore
17041709
if emit_count == 0:
17051710
return []
1706-
return [i for i in struct.unpack(f"{emit_count}i", buffer.read(size=emit_count * 4))]
1711+
return [i for i in struct.unpack(f"{emit_count}i", buffer.read(size=emit_count * 4))] # type: ignore
17071712

17081713

17091714
class SpriteListTextureData(SpriteListData):
@@ -1892,25 +1897,30 @@ def get_nearby_sprite_indices(self, pos: Point, size: Point, length: int) -> lis
18921897
ctx = self.ctx
18931898
if ctx._gl_api == "webgl":
18941899
raise RuntimeError("GPU Collision is not supported on WebGL Backends")
1900+
1901+
# All of these type ignores are because of GPU collision not being supported on WebGL
1902+
# Unfortuantely the type checkers don't have a sane way of understanding that, and it's
1903+
# not worth run-time checking all of these things, because they are guaranteed based on
1904+
# active GL api of the context. Pyright actually does seem to be able to figure it out
1905+
# but mypy does not
1906+
18951907
buffer = ctx.collision_buffer
18961908
program = ctx.collision_detection_program_simple
1897-
program["check_pos"] = pos
1898-
program["check_size"] = size
1909+
program["check_pos"] = pos # type: ignore
1910+
program["check_size"] = size # type: ignore
18991911

19001912
self._storage_pos_angle.use(0)
19011913
self._storage_size.use(1)
19021914
self._storage_index.use(2)
19031915

1904-
with ctx.collision_query:
1905-
# This is ignored because it is guaranteed to not be none by the above context GL api
1906-
# check. This is only able to be None when we are on WebGL.
1916+
with ctx.collision_query: # type: ignore
19071917
ctx.geometry_empty.transform(
1908-
program,
1918+
program, # type: ignore
19091919
buffer, # type: ignore
19101920
vertices=length,
19111921
)
1912-
emit_count = ctx.collision_query.primitives_generated
1922+
emit_count = ctx.collision_query.primitives_generated # type: ignore
19131923
# print(f"Collision query emitted {emit_count} sprites")
19141924
if emit_count == 0:
19151925
return []
1916-
return [i for i in struct.unpack(f"{emit_count}i", buffer.read(size=emit_count * 4))]
1926+
return [i for i in struct.unpack(f"{emit_count}i", buffer.read(size=emit_count * 4))] # type: ignore

0 commit comments

Comments
 (0)