Skip to content

[Hotfix] Make Use class callable to resolve mypy type errors with And/Or #337

Merged
skorokithakis merged 4 commits into
keleshev:masterfrom
qequ:hotfix-use-type
Oct 21, 2025
Merged

[Hotfix] Make Use class callable to resolve mypy type errors with And/Or #337
skorokithakis merged 4 commits into
keleshev:masterfrom
qequ:hotfix-use-type

Conversation

@qequ

@qequ qequ commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

When using Use with And or Or combinators, mypy reported a type error:

from schema import And, Use

schema = And(int, Use(int))

Error:

Argument 2 to "And" has incompatible type "Use"; expected "Callable[..., Any]"

Solution

Added __call__ method to the Use class, making it properly callable:

def __call__(self, data: Any) -> Any:
    return self._callable(data)

Closes #336

qequ added 4 commits October 16, 2025 14:10
Signed-off-by: Alvaro Frias <alvaro.frias@eclypsium.com>
Signed-off-by: Alvaro Frias <alvaro.frias@eclypsium.com>
Signed-off-by: Alvaro Frias <alvaro.frias@eclypsium.com>
Signed-off-by: Alvaro Frias <alvaro.frias@eclypsium.com>
@skorokithakis

Copy link
Copy Markdown
Collaborator

Hmm, how did calling work before without the class being callable?

@qequ

qequ commented Oct 17, 2025

Copy link
Copy Markdown
Contributor Author

there was no call definition but this is mostly a static type check error in execution time there are no changes. the implementation of call seems more suitable given that is a wrapper of the callable being used but it could be left unimplemented and I think mypy would pass given that it would be callable

@skorokithakis

Copy link
Copy Markdown
Collaborator

Hmm, I'm confused. The proper way to use Use before this was to call it, no? How did that work without a call definition?

@qequ

qequ commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

I don't think so given that the Use class has the validate method and that calls the callable method it wraps and in the _priority method its checked first that has the validate method than the it's callable (https://github.com/keleshev/schema/blob/master/schema/__init__.py#L335) Use class will always be used via validate method that's why didn't need the __call__ implementation

@skorokithakis

Copy link
Copy Markdown
Collaborator

Makes sense, thank you!

@skorokithakis skorokithakis merged commit 6d3d7bc into keleshev:master Oct 21, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type incompatibility introduced in version 0.7.8

2 participants