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

Issue/363 type hints #435

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

bennettscience
Copy link
Contributor

@bennettscience bennettscience commented Oct 30, 2020

Proposed Changes

Include type hints for the <Canvas> class.

This is a first draft of including type annotations in the library. The contribution guidelines have also been updated to show how to add annotations to other modules following Python 3.6 formatting for backwards compatibility. I've tagged the original issue for reference, but this PR only includes annotations for <Canvas> at this point.

Related issue: #363

Added new information on type hinting in the contribution guidelines.
Includes examples for contributors who have never added type hints to
maintain consistency.

Includes a section on remaining Python 3.6 compatible with type hints.
The `<Canvas>` module has had Python 3.6 compliant hints added. This
will allow IDEs to display type hints to users as they work. The
contribution guidelines have been updated to show the preferred
method of adding type annotations gradually.
@bennettscience
Copy link
Contributor Author

So, this failed because of the mdl check on spaces after list markers. The rule specifies 1, the contribution guidelines all use 3. Do you want to reformat for the standard rule or write our own for 3 spaces after a list marker?

@Thetwam Thetwam added this to the CanvasAPI v2.1.0 milestone Nov 1, 2020
@Thetwam Thetwam added the hacktoberfest-accepted Indicates valid PRs for Hacktoberfest that we might not quite be ready to merge in yet. label Nov 1, 2020
@Thetwam Thetwam removed this from the CanvasAPI v2.1.0 milestone Nov 30, 2020
@Thetwam Thetwam removed the hacktoberfest-accepted Indicates valid PRs for Hacktoberfest that we might not quite be ready to merge in yet. label Feb 4, 2021
@jessemcbride jessemcbride added this to the CanvasAPI v2.2.0 milestone Feb 22, 2021
@Thetwam Thetwam removed this from the CanvasAPI v2.2.0 milestone Mar 24, 2021
Copy link
Member

@jessemcbride jessemcbride left a comment

Choose a reason for hiding this comment

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

Hey @bennettscience, thank you for this patch. I'm sorry for the radio silence on it.

I've had an opportunity to experiment with mypy in some other projects and I think I finally have some useful feedback. Please take a look at the comments and let me know what you think when you get a chance.

return AppointmentGroup(self.__requester, response.json())
```

To get around this problem, the `typing` package has a `TYPE_CHECKING` module
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to use forward references instead of conditional importing? I notice you use them later, but I'm not sure if the conditional imports are a requirement for forward references to work or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd tried that, but if I remember correctly, I had inconsistent results...mypy still threw errors. But, I could be remembering that wrong. I'll pull the last update back into this branch and see what happens.

@@ -230,7 +250,9 @@ def create_appointment_group(self, appointment_group, **kwargs):

return AppointmentGroup(self.__requester, response.json())

def create_calendar_event(self, calendar_event, **kwargs):
def create_calendar_event(
self, calendar_event: dict, **kwargs: Union[str, dict, Optional[dict]]
Copy link
Member

Choose a reason for hiding this comment

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

Matt and I have had a few conversations about this and we're not sure what the best approach is: how do we handle kwargs?

One of the nice things about taking in arbitrary keyword arguments is that canvasapi can maintain parity with Canvas updates as long as the endpoints themselves don't change. The parameters might change types or names or expect new values, but there's nothing we have to do downsteam.

Of course, the downside to that is that there's no useful information for developers in the function signature about what Canvas might accept. Typehints partially address this, but they reintroduce the API parity problem.

Unless we can come up with a good tradeoff, I wonder if our kwargs typehint needs to be Any or something equally permissive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I did a lot of reading on how to type **kwargs and there isn't really a broad consensus. For me, I'm typically passing those in as a dict, but Any also makes sense. It's not considered bad practice because it really could be anything. Maybe a note in the docs somewhere about the Any type being limited to kwargs specifically?

@@ -498,7 +545,7 @@ def get_appointment_group(self, appointment_group, **kwargs):
)
return AppointmentGroup(self.__requester, response.json())

def get_appointment_groups(self, **kwargs):
def get_appointment_groups(self, **kwargs: Optional[dict]) -> PaginatedList:
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to annotate with PaginatedList[AppointmentGroup]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you can do that directly or through a Type Alias to simplify complex returns.

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.

3 participants