Skip to content
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

TypeError: Cannot coerce {'mandatory': True} into <class 'str'> #740

Open
Tracked by #1106
ghisvail opened this issue Mar 22, 2024 · 5 comments
Open
Tracked by #1106

TypeError: Cannot coerce {'mandatory': True} into <class 'str'> #740

ghisvail opened this issue Mar 22, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@ghisvail
Copy link
Collaborator

This snippet used to work with version 0.22:

from pydra.engine import Worflow
from pydra.engine.specs import BaseSpec, SpecInfo

input_spec = SpecInfo(name="Input", fields=[("T1w", str, {"mandatory": True})], bases=(BaseSpec,))
workflow = Workflow(name="foo", input_spec=input_spec)

But fails with version 0.23 with a coercion error:

TypeError: Cannot coerce {'mandatory': True} into <class 'str'> in 'T1w' field of Input as the coercion doesn't match any of the explicit inclusion criteria: Sequence -> Sequence, Mapping -> Mapping, Path -> PathLike, str -> PathLike, PathLike -> Path, PathLike -> str, Any -> MultiInputObj, int -> float, Integer -> float, int -> Decimal, Boolean -> bool, Decimal -> float, Integer -> int, Text -> str, bool -> Boolean, float -> Decimal, int -> Integer, str -> Text, integer -> int, floating -> float, bool_ -> bool, integer -> float, character -> str, complexfloating -> complex, bytes_ -> bytes, ndarray -> Sequence, Sequence -> ndarray

Suggesting that fields with metadata are incorrectly fed to the type coercion functionality.

@ghisvail ghisvail added the bug Something isn't working label Mar 22, 2024
@ghisvail
Copy link
Collaborator Author

Okay, here the metadata dictionary is wrongly passed as the default value.

@ghisvail
Copy link
Collaborator Author

Looks like the make_klass helper uses the presence of the help_string key to distinguish between a field defined with a triple (name, type, default) and (name, type, metadata), hence the bug.

@ghisvail
Copy link
Collaborator Author

ghisvail commented Mar 22, 2024

It does not look like the logic in make_klass has changed much with this commit, so it was probably discarded silently somewhere further in the code before 🤔

Not sure what to make of this bug. Will adjust the offending test in Clinica meanwhile.

@tclose
Copy link
Contributor

tclose commented Mar 24, 2024

I think this is just a case of type-checking uncovering an existing bug.

I have run into the missing help_string -> 3rd arg == default issue before and it is pretty confusing. I reckon we should have a look at changing that behaviour if we end up making any changes to the API

@ghisvail
Copy link
Collaborator Author

ghisvail commented Apr 3, 2024

Supporting multiple triples whose content of the last attribute decides what it means, is a recipe for disaster imo. Since metadata is pretty much required to provide help_string, then the only sensible choices may be:

  • pair (name, metadata): type inferred as str
  • triple (name, type, metadata): what most will use
  • quadruple (name, type, default, metadata): add default value

Maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants