Rewrite MPS parsing to use dataclasses instead of dicts#831
Rewrite MPS parsing to use dataclasses instead of dicts#831pchtsp merged 7 commits intocoin-or:masterfrom
Conversation
|
how to you feel about using TypedDicts instead of dataclasses? This way we keep dictionaries and still have type checking without needing extra dependencies. |
|
I think dataclasses are the better approach. Three other options:
|
|
Understood. Let's just add the |
|
I'm taking a look at how much extra code it might be to implement converting dicts to a dataclass: @dataclass
class MPSParameters:
name: str
sense: int
status: int
sol_status: int
@classmethod
def fromDict(cls, data: dict[str, Any]) -> MPSParameters:
return cls(data["name"], data["sense"], data["status"], data["sol_status"])Each of the MPS dataclasses will just need to manually parse the dict. |
pchtsp
left a comment
There was a problem hiding this comment.
Thanks for this, it's looking very good. I see that you added some from_dict and fromDict to cover the previous methods in some cases. But I think that many classes have lost them. Can you check and add the (now useless) fromDict, from_dict, toDict and to_dict. Ideally, with some kind of deprecation warning? So that we can take them out in a next version.
For example, LpVariable is missing from_dict and fromDict methods. LpProblem is missing to_dict and from_dict.
Others are missing more.
I hate to being picky about this but I already had issues removing an undocumented alias I had for a method and someone, somewhere came back to complain I did not add a deprecation warning before removing it.
| to_dict = toDict | ||
|
|
||
| @classmethod | ||
| def fromDict(cls, dj=None, varValue=None, **kwargs): |
There was a problem hiding this comment.
I think we're missing the fromDict methods somewhere. Just for deprecation reasons, even if they're not useful anymore in pulp.
|
Do you want all |
|
I think the Thanks again. |
pchtsp
left a comment
There was a problem hiding this comment.
a typo in a warning. Besides that I think it's ready to merge. Right?
pulp/pulp.py
Outdated
| :rtype: list | ||
| """ | ||
| warnings.warn( | ||
| "LpAffineExpression.to_dict is deprecated, use LpAffineExpression.toDataclass instead", |
There was a problem hiding this comment.
I think here it should say: "... use LpAffineExpression.toDict instead"
|
Yes, I think all good to merge. |
In the spirit of breaking out changes from #807, this PR includes rewriting parsing MPS files to use dataclasses instead of dicts.
This change does not add any new capabilities, and was done to:
As raised in #807, a downside is that this adds a dependency on dacite, whereas there were not previously any (depending on which backend is in use).