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

Type checker reports error when using the Use class in a boolean operator #322

Open
PleasantFuban opened this issue Jul 23, 2024 · 7 comments · May be fixed by #326
Open

Type checker reports error when using the Use class in a boolean operator #322

PleasantFuban opened this issue Jul 23, 2024 · 7 comments · May be fixed by #326

Comments

@PleasantFuban
Copy link

Here is a minimal example:

schema = Schema({
    'party': And(str, Use(str.lower), lambda s: s in ('alice', 'bob'))
})

This code works fine. However, the type checker reports an error saying that a Use instance cannot be used as an argument of And(). This is true in both VS Code (which uses pyright by default) and PyCharm.

I'm using similar validation rules in many places. As a result, my code is full of # type: ignores because otherwise I will have to disable type checking completely.

Is it possible to solve this issue?

@mutricyl
Copy link

I am not able to reproduce your issue. Is mypy also complaining ? what version of python/schema are you using ?

@PleasantFuban
Copy link
Author

PleasantFuban commented Aug 23, 2024

Mypy does not complain in that minimal example (even when in strict mode), but does complain when moving And out of the schema:

And(str, Use(str.lower), lambda s: s in ('alice', 'bob'))
# Mypy reports: Argument 2 to "And" has incompatible type "Use"; expected "Callable[..., Any]"
# VS Code (pyright) reports: The type 'Use' is incompatible with the types 'Schema' and '(...) -> Any'
# PyCharm reports something similar to VS Code

{'party': And(str, Use(str.lower), lambda s: s in ('alice', 'bob'))}
# Mypy still complains

Schema({'party': And(str, Use(str.lower), lambda s: s in ('alice', 'bob'))})
# Mypy does not report anything

I haven't used Mypy before, so I'm not sure if it is a bug.

I'm using schema 0.7.7 on Python 3.12. Mypy's version 1.11.1.

@mutricyl
Copy link

ok, I now have the same issue. No solution yet but as a dirty workaround you can replace

Use(str.lower)

with

eval("Use(str.lower)")

@HorridModz
Copy link

ok, I now have the same issue. No solution yet but as a dirty workaround you can replace

Use(str.lower)

with

eval("Use(str.lower)")

That's really ugly. Why not just ignore the error?

# type: ignore

@HorridModz
Copy link

@mutricyl By the way, not to criticize, but I noticed your markdown code snippet looks like this (copy and pasted from your comment):

> ```python
> print("Hello World!")
> ```

Those greater than symbols are unnecessary. Just typing ``` is sufficient:

```python
print("Hello World!")
```

Hope this helps!

@PleasantFuban
Copy link
Author

ok, I now have the same issue. No solution yet but as a dirty workaround you can replace

Use(str.lower)

with

eval("Use(str.lower)")

Understood, thank you. Now I've put all schemas in a single file, so I can just disable type checking for that file. I'm comfortable with this.

@mutricyl
Copy link

I agree that the eval trick is ugly. You can narrow the type ignore to the specific issue : # type: ignore[call-arg] likewise if pyright complains: # pyright: ignore[<whateverPyrightIssueName>]

Apparently mypy does not consider Use as being a callable. I have added a __call__ function to Use class and it looks to solve the issue.

class Use:
 
(...)

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        pass

However I still have an issue mixing And and Or.

mutricyl pushed a commit to mutricyl/schema that referenced this issue Aug 27, 2024
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 a pull request may close this issue.

3 participants