Skip to content

Conversation

@davidmrdavid
Copy link
Collaborator

@davidmrdavid davidmrdavid commented Jul 17, 2020

Well typed programs don't go wrong ....

Do not merge yet, I want to double check with some of our internal Python experts if our use of the Optional type hint, and type aliasing is idiomatic. However, feel free to review

This PR constitutes, to the best of my knowledge, the first type-checking of our codebase. As per the mypy type-checker, our codebase is type-safe and. Most of the work required to get there revolved around acknowledging and guarding against the None types that flow through our code. In fact, after this effort, I'm really concerned about the amount of None initialization that we do in our codebase. As a result, I'd love to speak with some of our internal Python experts about providing sane initialization values to non-object types. Especially because initializing all optional values as None is, on the flipside, very convenient for guarding against them.

Something else I attempt in this PR, although very minimally, is the idea of introducing type aliases to provide more information around primitive types. For instance, many functions returns URL-formatted strings and thus the return type of str is not very informative, in my opinion. In languages like Haskell, it's common to alias these types to better document functions, therefore resulting in an aliasing of str to a new name like URLstr. I have yet to figure out how pythonic that really is. In this PR, I uses a type alias for Any called SerializableToJSON in hopes of conveying that some inputs can be of any type so long as they're serializable. Again, I need to check with an expert if this is idiomatic. If so, I'll propagate the SerializableToJSON datatype all over the codebase

@cgillum cgillum marked this pull request as draft July 17, 2020 01:08
@davidmrdavid davidmrdavid marked this pull request as ready for review July 17, 2020 05:04
Copy link

@ConnorMcMahon ConnorMcMahon left a comment

Choose a reason for hiding this comment

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

Looks mostly good, but I have some questions, likely due to me not knowing how type annotations in Python work.

instance_id=instance_id, orchestration_function_name=orchestration_function_name)

response = await self._post_async_request(request_url, self._get_json_input(client_input))
response: List[SerializableToJSON] = await self._post_async_request(

Choose a reason for hiding this comment

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

What was the conclusion about SerializableToJSON. I'm not sure about how Python feels about custom types like this to represent attributes of a type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I recently learned that the right approach here is to use typing Protocols [https://mypy.readthedocs.io/en/stable/protocols.html] which allows us to typecheck if some type exports some method. If so, we can implement SerializableToJSON as the union of some primitive types plus objects that export to_json + from_json methods.

However, that's a longer term goal. For now, I've changed SerializableToJSON back to Any, with the goal of merging this and revisiting the type in the future :)

Copy link

@ConnorMcMahon ConnorMcMahon left a comment

Choose a reason for hiding this comment

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

I think it looks good, just a couple last things to consider.

@davidmrdavid davidmrdavid merged commit 92fbd6f into dev Aug 5, 2020
@davidmrdavid davidmrdavid deleted the dajusto/intellisense branch August 5, 2020 17:22
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