sourcebots / robot-api

(Legacy) API to interface with robotd
http://docs.sourcebots.co.uk/api/
MIT License
4 stars 1 forks source link

Type checking! #34

Closed RealOrangeOne closed 6 years ago

RealOrangeOne commented 6 years ago

Add type checking, powered by MyPy 🎉

RealOrangeOne commented 6 years ago

Turns out the syntax i'm using for types is only supported in python3.6+, will refactor to use comment-based types instead

PeterJCLaw commented 6 years ago

Note: this PR is likely to conflict heavily with #41. Since this PR already has some conflicts, I suggest we merge that one first and then rework this PR.

I'm also tempted to use flake8-mypy to pull the type checking into the linting, though I've not explored whether that actually works before.

RealOrangeOne commented 6 years ago

@PeterJCLaw @prophile looking through all the changes here, there's a large amount of ambiguity the raw data that comes back from robotd. Do you know if there's any way we can type explicitly what keys / values to expect in that data, and their types? Or this a limitation Python hasnt solved yet?

Alternatively, we could replace the dictionary with a NamedTuple, which does support specifying keys and types as we need.

PeterJCLaw commented 6 years ago

I'm not aware of a way to specify a dictionary with a given set of keys. The reality is that somewhere has to deal with the fact that the JSON blogs are coming back from robotd are merely dictionaries and don't have type information.

One approach might be to explicitly convert the JSON into NamedTuples immediately and then wrap those in our helper classes as needed (is that what you had in mind?), though that feels pretty close to what we're already doing.

PeterJCLaw commented 6 years ago

It turns out that mypy is experimenting with a way to have dictionaries with specified keys. It's called TypedDict.