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

Adopt sp-repo-review #89

Merged
merged 9 commits into from
Oct 15, 2023
Merged

Adopt sp-repo-review #89

merged 9 commits into from
Oct 15, 2023

Conversation

blink1073
Copy link
Contributor

@Zsailer heads up, I dropped the use of inspect.signature since it doesn't mix well with mypy when from __future__ import annotations is used. Now that we're embracing typing, the type checker should be the one enforcing the signature.

Comment on lines 24 to 37
schema = """
$id: http://myapplication.org/my-method
version: 1
title: My Method Executed
description: My method was executed one time.
properties:
msg:
title: Message
type: string
"""

self.eventlogger.register_event_schema(
schema=schema
)
Copy link
Member

Choose a reason for hiding this comment

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

The indent here was intentional, since it's part of the method from the preview code chunk.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 43 to 49
def my_method(self):
# Do something
...
# Emit event telling listeners that this event happened.
self.eventlogger.emit(schema_id="myapplication.org/my-method", data={"msg": "Hello, world!"})
# Do something else...
...
Copy link
Member

Choose a reason for hiding this comment

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

Same here. This indent is intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, this was auto-formatted by blacken-docs, maybe we need to skip this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by skipping

@Zsailer
Copy link
Member

Zsailer commented Oct 10, 2023

Thanks, @blink1073!

I dropped the use of inspect.signature since it doesn't mix well with mypy when from future import annotations is used. Now that we're embracing typing, the type checker should be the one enforcing the signature.

Thanks for pointing this out.

I added this logic to catch invalid function at runtime and raise a clear error, since 1) it's possible (and not uncommon) to define a listener/modifier function after a live instance of this class has been created and 2) it's way too easy to define an invalid function. 😅

We lose this by relying on (static) type checker's right? There is no way to validate a function signature when it's defined after the start of runtime?

@blink1073
Copy link
Contributor Author

Actually, here's a thought. If the parameters are non-string, we use inspect.signature. If they are strings, we defer to mypy.

@Zsailer
Copy link
Member

Zsailer commented Oct 10, 2023

Looking at this again, I don't follow what the issue is.

The typing you added looks great to me. Why does the inspect.signature logic impact mypy?

@blink1073
Copy link
Contributor Author

I couldn't use from __future__ import annotations in this file in the server PR.

Here's an illustration:

In [1]: async def listener_signature_str_ann(
   ...:             logger: "EventLogger", schema_id: "str", data: "dict"
   ...:         ) -> "None":
   ...:             """An interface for a listener."""
   ...:             ...
   ...:

In [2]: async def listener_signature(logger: object, schema_id: str, data: dict) -> None:
   ...:             """An interface for a listener."""
   ...:             ...
   ...:

In [3]: import inspect

In [4]: inspect.signature(listener_signature)
Out[4]: <Signature (logger: object, schema_id: str, data: dict) -> None>

In [5]: inspect.signature(listener_signature_str_ann)
Out[5]: <Signature (logger: 'EventLogger', schema_id: 'str', data: 'dict') -> 'None'>

In [10]: inspect.signature(listener_signature_str_ann).parameters['logger'].annotation
Out[10]: 'EventLogger'

In [11]: inspect.signature(listener_signature).parameters['logger'].annotation
Out[11]: object

@blink1073
Copy link
Contributor Author

In #88 I added support for an annotated string, but it only works for dict, not Dict, and not dict[str, Any], etc. It is too brittle.

@Zsailer
Copy link
Member

Zsailer commented Oct 11, 2023

Oh, I see. Yeah, you're right.

Let's drop the inspect.signature. I'm convinced that the type annotation and checker is enough to help developers interact with this API. 👍

Thank you for doing this work!

@blink1073 blink1073 enabled auto-merge (squash) October 15, 2023 13:56
@blink1073 blink1073 merged commit e3edb6a into jupyter:main Oct 15, 2023
22 checks passed
@blink1073 blink1073 deleted the sp-repo-review branch October 15, 2023 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants