-
Notifications
You must be signed in to change notification settings - Fork 9
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
Type hint and validate Instance and Solution dictionaries #100
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e9d045
Add Instance TypedDict, improve typing
leonlan a8a16a6
Ignore variable as key in TypedDict
leonlan acfa96f
Fix parse solution test
leonlan b6f1df3
Add type hint in depot
leonlan 11696e2
Accept os.Pathlike in read
leonlan f72e884
Fix depot type
leonlan 4bbede5
Remove type from return info in docstring
leonlan ca4a029
Remove depot int type
leonlan f9c5f18
Remove union from depot type
leonlan 0074092
Separate Instance/Solution to files
leonlan 30e322e
Discard changes to vrplib/read/read_solution.py
leonlan 07b6461
Discard changes to vrplib/read/read_instance.py
leonlan f28e057
Merge branch 'master' into improve-typing
leonlan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import TypedDict | ||
|
||
import numpy as np | ||
|
||
|
||
class Instance(TypedDict, total=False): | ||
# Specifications | ||
name: str | ||
dimension: int # number of depots + number of customers | ||
edge_weight_format: str | ||
edge_weight_type: str | ||
display_type: str | ||
comment: str | ||
vehicles: int # number of vehicles | ||
capacity: int | ||
|
||
# Data sections | ||
node_coord: np.ndarray | ||
demand: np.ndarray | ||
depot: np.ndarray | ||
service_time: np.ndarray | ||
time_window: np.ndarray | ||
edge_weight: np.ndarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from typing import List, TypedDict | ||
|
||
|
||
class Solution(TypedDict, total=False): | ||
routes: List[List[int]] | ||
cost: float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypedDict requires string literals, but since I don't know what kinds of section are parsed I need to ignore this.
I'm not sure if it even makes sense to use a TypedDict if I ignore this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's OK. Even though this line is no longer checked, the returned instance is assumed to be of type
Instance
, which used by mypy to do static type checking.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if we do this, we also need to validate that the instance indeed follows the types as specified in
Instance
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can use
pydantic
for this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a new
validate
module that validates the instance and solution outputs according to VRPLIB.