Skip to content

Conversation

eltoder
Copy link
Contributor

@eltoder eltoder commented Jul 1, 2025

There are 2 issues with the annotations:

  1. Function types are contravariant rather than covariant in their argument types. This means that Callable[[BaseXmlModel], Any] is a subtype rather than a supertype of Callable[[MyModel], Any] and so using Callable[[BaseXmlModel], Any] as a lower bound of TypeVars doesn't work. Since Python doesn't support upper bounds on TypeVars, the solution is to push the TypeVar inside. To see the issue run mypy on the example from the documentation[1]. Currently, you get these errors:

    t.py:13: error: Value of type variable "ValidatorFuncT" of function cannot be "Callable[[type[Plot], XmlElementReader, str], list[float]]"  [type-var]
    t.py:21: error: Value of type variable "SerializerFuncT" of function cannot be "Callable[[Plot, XmlElementWriter, list[float], str], None]"  [type-var]
    

    This PR resolves these (however, validate_space_separated_list needs to be decorated with @classmethod).

  2. A recent commit[2] has a typo where ValidatorFuncT was bound by SerializerFunc instead of ValidatorFunc.

[1] https://pydantic-xml.readthedocs.io/en/latest/pages/misc.html#custom-xml-serialization
[2] ec4b547

@eltoder eltoder force-pushed the feature/fix-validator-annotations branch 2 times, most recently from f34315c to 6357e5b Compare July 2, 2025 03:01
@eltoder
Copy link
Contributor Author

eltoder commented Jul 2, 2025

@dapper91 the checks fail because PRs opened from forks don't get access to your codecov token, and so uploading of coverage information fails. You appear to be using a very old version of codecov action. If you upgrade to a newer one, this should work: https://docs.codecov.com/docs/codecov-tokens#commits-on-unprotected-branches

@eltoder eltoder force-pushed the feature/fix-validator-annotations branch from 6357e5b to 8bd9725 Compare July 2, 2025 03:36
@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.31%. Comparing base (526259e) to head (bc23aa1).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #277   +/-   ##
=======================================
  Coverage   92.30%   92.31%           
=======================================
  Files          30       30           
  Lines        1728     1730    +2     
=======================================
+ Hits         1595     1597    +2     
  Misses        133      133           
Flag Coverage Δ
unittests 92.31% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eltoder
Copy link
Contributor Author

eltoder commented Jul 2, 2025

@dapper91 I upgraded codecov action to fix the failure

@eltoder eltoder changed the base branch from master to dev July 2, 2025 03:49
@eltoder eltoder force-pushed the feature/fix-validator-annotations branch from 8bd9725 to dd221f4 Compare July 2, 2025 03:51
There are 2 issues with the annotations:

1. Function types are contravariant rather than covariant in their
   argument types. This means that `Callable[[BaseXmlModel], Any]` is a
   subtype rather than a supertype of `Callable[[MyModel], Any]` and so
   using `Callable[[BaseXmlModel], Any]` as a lower bound of TypeVars
   doesn't work. Since Python doesn't support upper bounds on TypeVars,
   the solution is to push the TypeVar inside. To see the issue run mypy
   on the example from the documentation[1]. Currently, you get these
   errors:

```
t.py:13: error: Value of type variable "ValidatorFuncT" of function cannot be "Callable[[type[Plot], XmlElementReader, str], list[float]]"  [type-var]
t.py:21: error: Value of type variable "SerializerFuncT" of function cannot be "Callable[[Plot, XmlElementWriter, list[float], str], None]"  [type-var]
```

   This PR resolves these (however, validate_space_separated_list needs
   to be decorated with @classmethod).

2. A recent commit[2] has a typo where ValidatorFuncT was bound by
   SerializerFunc instead of ValidatorFunc.

[1] https://pydantic-xml.readthedocs.io/en/latest/pages/misc.html#custom-xml-serialization
[2] dapper91@ec4b547
@eltoder eltoder force-pushed the feature/fix-validator-annotations branch from dd221f4 to bc23aa1 Compare July 2, 2025 05:00
@dapper91 dapper91 merged commit 43c45b4 into dapper91:dev Jul 13, 2025
6 checks passed
@eltoder eltoder deleted the feature/fix-validator-annotations branch July 13, 2025 15:58
@dapper91 dapper91 mentioned this pull request Jul 13, 2025
Merged
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.

3 participants