Skip to content

Construct instance of appropriate class in Event._init #752

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shwina
Copy link
Contributor

@shwina shwina commented Jul 10, 2025

Description

Closes #750

closes

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

Copy link
Contributor

copy-pr-bot bot commented Jul 10, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@shwina
Copy link
Contributor Author

shwina commented Jul 11, 2025

/ok to test eb6890b

@@ -89,7 +89,7 @@ cdef class Event:

@classmethod
def _init(cls, device_id: int, ctx_handle: Context, options=None):
cdef Event self = Event.__new__(Event)
cdef Event self = Event.__new__(cls)
Copy link
Contributor Author

@shwina shwina Jul 11, 2025

Choose a reason for hiding this comment

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

This ensures that a cls instance is produced here, which could be different from Event if cls is a subclass.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this works! I was trying:

cdef cls self = Event.__new__(cls)

and getting compile time errors.

Copy link

Copy link
Contributor

@carterbox carterbox left a comment

Choose a reason for hiding this comment

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

This same fix should also be applied to the Stream class.

@github-project-automation github-project-automation bot moved this from Todo to In Progress in CCCL Jul 11, 2025
@@ -89,7 +89,7 @@ cdef class Event:

@classmethod
def _init(cls, device_id: int, ctx_handle: Context, options=None):
cdef Event self = Event.__new__(Event)
cdef Event self = Event.__new__(cls)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a test for a derived class of Event to ensure that we don't accidentally regress on what this enables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can go about this in two ways:

  • Write a test that invokes MyEvent.__new__ directly and test that it produces a MyEvent (easy)
  • Write a test that monkeypatches Event to MyEvent, and test that constructing an event via Stream or Device produces a MyEvent rather than an Event (more complex, but representative of how users can actually use subclasses).

Ultimately, (2) calls down to (1) but in a way that's an implementation detail. Any preferences?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets take the easy path and we can always revisit in the future if we see value in doing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[FEA]: private constructor prevents derived classes of cythonized Stream, Event
3 participants