You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that the package uses if type(var) == float:, and thought it may be useful to modify the behaviour to be more Pydantic.
To summarise, isinstance caters for inheritance (where an instance of a derived class is an instance of a base class), while checking for equality of type does not. This instead demands identity of types and rejects instances of subclasses.
Typical Python code should support inheritance, so isinstance is less bad than checking types, as it supports inheritance. However, “duck typing” would be the preferred (try, except), catching all exceptions associated with an incorrect type (TypeError).
I refer to lines 142-153, whereby the list type is evaluated:
iftype(layer_structure) ==list:
self.layer_structure=layer_structureelse:
raiseValueError("Layer structure must be specified within a list")
which could be achieved more elegantly using:
ifnotisinstance(layer_structure, list):
raiseTypeError("Layer structure must be specified within a list.")
whereby the type (or types) could be hinted to the user within the init dunder method, and can be evaluated through:
ifisinstance(weight_decay, str):
ifweight_decay!='default':
raiseValueError("A warning that the value must be 'default' or a float type")
self.weight_decay=weight_decayelifisinstance(weight_decay, float):
self.weight_decay=weight_decay
Depending on the python versions supported, I would also recommend using typehints, and using the below:
fromtypingimportListabc_var: List[int]
More than happy to submit a PR with the proposed changes.
The text was updated successfully, but these errors were encountered:
Hi @David-Woroniuk, thanks for the excellent suggestion. It would be great if you could submit a PR request. And please do let us know if you have other ideas for improving the package!
Firstly, a great package.
I noticed that the package uses
if type(var) == float:
, and thought it may be useful to modify the behaviour to be more Pydantic.To summarise,
isinstance
caters for inheritance (where an instance of a derived class is an instance of a base class), while checking for equality oftype
does not. This instead demands identity of types and rejects instances of subclasses.Typical Python code should support inheritance, so
isinstance
is less bad than checking types, as it supports inheritance. However, “duck typing” would be the preferred (try, except), catching all exceptions associated with an incorrect type (TypeError).I refer to lines 142-153, whereby the list type is evaluated:
which could be achieved more elegantly using:
181-187:
whereby the type (or types) could be hinted to the user within the init dunder method, and can be evaluated through:
Depending on the python versions supported, I would also recommend using typehints, and using the below:
More than happy to submit a PR with the proposed changes.
The text was updated successfully, but these errors were encountered: