Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing type hints #1720

Draft
wants to merge 70 commits into
base: development
Choose a base branch
from

Conversation

gran4
Copy link
Contributor

@gran4 gran4 commented Apr 24, 2023

Ready to merge. @einarf the errors are either things like variable types undeclared or something I need help on. So pls review. Look at the windows test for the weird error I do not get.

arcade/application.py Outdated Show resolved Hide resolved
arcade/application.py Outdated Show resolved Hide resolved
arcade/application.py Outdated Show resolved Hide resolved
arcade/application.py Outdated Show resolved Hide resolved
arcade/context.py Show resolved Hide resolved
arcade/gui/property.py Outdated Show resolved Hide resolved
arcade/sprite/sprite.py Show resolved Hide resolved
Copy link
Member

@pushfoo pushfoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already getting to be a somewhat large PR. I'd turn this change into a separate PR as there may be additional changes related to the ticket.

Pyglet doesn't have __slots__ so I didn't know all the variables
@gran4
Copy link
Contributor Author

gran4 commented May 3, 2023

I'll keep those in becuase I don't know how to roll them back, but I'll put anything more into a different PR.
You know what.. ok.
There are a bunch of problems bc using Self.

@cspotcode cspotcode mentioned this pull request May 6, 2023
arcade/gui/property.py Outdated Show resolved Hide resolved
@cspotcode
Copy link
Collaborator

This should have the latest development branch merged into it.

Looking at the remaining errors:

arcade/math.py:199: error: Need type annotation for "vel"  [var-annotated]
arcade/math.py:237: error: Static methods cannot use Self type  [misc]
arcade/math.py:237: error: A function returning TypeVar should receive at least one argument containing the same TypeVar  [type-var]
arcade/math.py:237: note: Consider using the upper bound "_Vec2" instead
arcade/math.py:239: error: Incompatible return value type (got "_Vec2", expected "Self")  [return-value]
arcade/math.py:242: error: Incompatible return value type (got "_Vec2", expected "Self")  [return-value]
arcade/math.py:245: error: Incompatible return value type (got "_Vec2", expected "Self")  [return-value]
arcade/math.py:248: error: Incompatible return value type (got "_Vec2", expected "Self")  [return-value]
arcade/math.py:251: error: Incompatible return value type (got "_Vec2", expected "Self")  [return-value]

@pushfoo On Vec2, it turns out that using Self as return type is actually wrong. If you subclass Vec2 as MyVec2 and then trigger __add__ via MyVec2(0, 0) + MyVec2(0, 0) then what you get is a Vec2, not a MyVec2.

The correct return type annotation is thus Vec2, not Self. If we think this is a bug in Vec2, then that will happen in a separate PR. The goal for this PR should be for tests and linting to pass, which means changing the annotation to Vec2 or removing the annotation.


arcade/application.py:47: error: Name "Screen" is not defined  [name-defined]

Can be solved by adding Screen to imports: from pyglet.canvas.base import ScreenMode, Screen


arcade/sprite_list/sprite_list.py:1065: error: Item "None" of "Optional[Buffer]" has no attribute "orphan"  [union-attr]
arcade/sprite_list/sprite_list.py:1066: error: Item "None" of "Optional[Buffer]" has no attribute "orphan"  [union-attr]
arcade/sprite_list/sprite_list.py:1067: error: Item "None" of "Optional[Buffer]" has no attribute "orphan"  [union-attr]
arcade/sprite_list/sprite_list.py:1068: error: Item "None" of "Optional[Buffer]" has no attribute "orphan"  [union-attr]
arcade/sprite_list/sprite_list.py:1069: error: Item "None" of "Optional[Buffer]" has no attribute "orphan"  [union-attr]
arcade/shape_list.py:121: error: Item "None" of "Optional[Geometry]" has no attribute "render"  [union-attr]

This class of error is suppressed in our pyright config, so I'm not sure how we want to handle this. Turn off mypy? Turn off union-attr diagnostics? Add assertions? Add # type: ignore?

@pushfoo
Copy link
Member

pushfoo commented Jun 2, 2023

The correct return type annotation is thus Vec2, not Self.

Understood & agreed.

If we think this is a bug in Vec2, then that will happen in a separate PR.

It's _Vec2, not Vec2, and that only makes your point stronger. The changes I could suggest to make this more flexible and inheritance safe are irrelevant since:

  1. This is an internal class which only has a single current use: the particle emitter
  2. We might be able to replace the usage with a shader-based particle system or just use pyglet's Vec2.

Even if we don't replace this class and think it's useful, Self is mostly helpful for subclassing. However, having a stable internal class around could be useful to protect against API changes in pyglet's vectors.

This class of error is suppressed in our pyright config, so I'm not sure how we want to handle this.

I'm not sure either. The # type: ignore is awful but it'll get things done quickly and run well. I knowassert has been controversial before, so are any of the other alternatives performant?

@gran4
Copy link
Contributor Author

gran4 commented Jun 3, 2023

I'm not sure either. The # type: ignore is awful but it'll get things done quickly and run well. I knowassert has been controversial before, so are any of the other alternatives performant?

I added that for now.

@einarf
Copy link
Member

einarf commented Jun 8, 2023

Still assuming this is a WIP

@gran4 gran4 requested a review from cspotcode July 5, 2023 21:06
Comment on lines +582 to +593
for i in range(len(point_list) - 1):
point1 = point_list[i]
point2 = point_list[i + 1]

points = get_points_for_thick_line(point1[0], point1[1], point2[0], point2[1], line_width)
triangle_point_list.extend([points[1], points[0], points[2], points[3]])

# Handle the last segment (connecting back to the start)
point1 = point_list[-1]
point2 = point_list[0]
points = get_points_for_thick_line(point1[0], point1[1], point2[0], point2[1], line_width)
triangle_point_list.extend([points[1], points[0], points[2], points[3]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why is this in a type hint PR?
  2. Do we have unit tests covering these changes?

If this is a needed improvement. If it works, this should be in another, separate PR.

@eruvanos eruvanos marked this pull request as draft November 8, 2023 21:32
@eruvanos
Copy link
Member

eruvanos commented Nov 8, 2023

Can not be merged right now, I changed it to be a draft.

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.

None yet

5 participants