Skip to content

Conversation

@alexfikl
Copy link
Contributor

@alexfikl alexfikl commented Jul 6, 2025

Inspired by all the new errors in #616, this adds types to pretty much everything in tools.py (on a bit of a best effort basis, since some of them may be too strict). Some small-ish parts are still missing:

  • type checking for get_gl_sharing_context_properties, mostly due to missing the OpenGL bindings.
  • np.dtype doesn't seem to like taking a dict (in match_dtype_to_c_struct), not sure why.
  • mako still isn't typed and causing some small errors.
  • Some issues around cl.array.Array.data not having the right type. This might just need some checking to make sure it's ok.

@alexfikl alexfikl marked this pull request as draft July 6, 2025 13:51
@alexfikl alexfikl force-pushed the type-tools branch 2 times, most recently from 495ec95 to 3d7d45e Compare July 6, 2025 16:29
@alexfikl alexfikl marked this pull request as ready for review July 6, 2025 16:29
@inducer
Copy link
Owner

inducer commented Jul 7, 2025

Found a way that avoids all the casting.

Btw, given the choice between cast and assert, I strongly prefer cast for cases where we're just providing info to the type checker. If you're worried about overheads, I think it'd be relatively straightforward to statically remove all the casts in an import hook or so.

@alexfikl
Copy link
Contributor Author

alexfikl commented Jul 7, 2025

Found a way that avoids all the casting.

Nice! I thought of doing something like that too, but wasn't sure it would work :(

Btw, given the choice between cast and assert, I strongly prefer cast for cases where we're just providing info to the type checker.

Hm, I usually went by "if I need to cast, it probably means I didn't narrow the types enough for the type checker" and tried to avoid it. Yours is probably a better way to look at it when gradually adding types to things. :-?

@inducer
Copy link
Owner

inducer commented Jul 7, 2025

Yours is probably a better way to look at it when gradually adding types to things. :-?

Not claiming it's a good way! And gradual typing mostly doesn't factor into it for the most part. Just trying to make sense of when to use what. Here's a different restatement:

  • assert if the condition is a useful check to have at run time.
  • cast for things the type checker needs to know but does not.

As a microbenchmark matter, the two have slightly different cost profiles, but I'm choosing to just regard both as "free", to avoid being pulled in spurious directions.

@inducer inducer merged commit b0d8c91 into inducer:main Jul 7, 2025
18 checks passed
@inducer
Copy link
Owner

inducer commented Jul 7, 2025

Thx!

@alexfikl alexfikl deleted the type-tools branch July 7, 2025 19:11
@alexfikl
Copy link
Contributor Author

alexfikl commented Jul 7, 2025

Just trying to make sense of when to use what.

I'll happily defer to your newly gained extensive experience in typing 😁

As a microbenchmark matter, the two have slightly different cost profiles, but I'm choosing to just regard both as "free", to avoid being pulled in spurious directions.

I can definitely agree with that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants