-
Notifications
You must be signed in to change notification settings - Fork 114
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
typeguard is unable to check for explicit UnionType class #467
Labels
Comments
I believe the following fix to the
|
github-actions bot
added a commit
to Jij-Inc/Playground
that referenced
this issue
Nov 4, 2024
Bumps [typeguard](https://github.com/agronholm/typeguard) from 4.4.0 to 4.4.1. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/agronholm/typeguard/releases">typeguard's releases</a>.</em></p> <blockquote> <h2>4.4.1</h2> <ul> <li>Dropped Python 3.8 support</li> <li>Changed the signature of <code>typeguard_ignore()</code> to be compatible with <code>typing.no_type_check()</code> (PR by <a href="https://github.com/jolaf"><code>@jolaf</code></a>)</li> <li>Avoid creating reference cycles when type checking uniontypes and classes</li> <li>Fixed checking of variable assignments involving tuple unpacking (<a href="https://github.com/agronholm/typeguard/issues/486">#486</a>)</li> <li>Fixed <code>TypeError</code> when checking a class against <code>type[Self]</code> (<a href="https://github.com/agronholm/typeguard/issues/481">#481</a>)</li> <li>Fixed checking of protocols on the class level (against <code>type[SomeProtocol]</code>) (<a href="https://github.com/agronholm/typeguard/issues/498">#498</a>)</li> <li>Fixed <code>Self</code> checks in instance/class methods that have positional-only arguments</li> <li>Fixed explicit checks of PEP 604 unions against <code>types.UnionType</code> (<a href="https://github.com/agronholm/typeguard/issues/467">#467</a>)</li> <li>Fixed checks against annotations wrapped in <code>NotRequired</code> not being run unless the <code>NotRequired</code> is a forward reference (<a href="https://github.com/agronholm/typeguard/issues/454">#454</a>)</li> </ul> </blockquote> </details> <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/agronholm/typeguard/blob/master/docs/versionhistory.rst">typeguard's changelog</a>.</em></p> <blockquote> <h1>Version history</h1> <p>This library adheres to <code>Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200></code>_.</p> <p><strong>4.4.1</strong> (2024-11-03)</p> <ul> <li>Dropped Python 3.8 support</li> <li>Changed the signature of <code>typeguard_ignore()</code> to be compatible with <code>typing.no_type_check()</code> (PR by <a href="https://github.com/jolaf"><code>@jolaf</code></a>)</li> <li>Avoid creating reference cycles when type checking uniontypes and classes</li> <li>Fixed checking of variable assignments involving tuple unpacking (<code>[#486](agronholm/typeguard#486) <https://github.com/agronholm/typeguard/issues/486></code>_)</li> <li>Fixed <code>TypeError</code> when checking a class against <code>type[Self]</code> (<code>[#481](agronholm/typeguard#481) <https://github.com/agronholm/typeguard/issues/481></code>_)</li> <li>Fixed checking of protocols on the class level (against <code>type[SomeProtocol]</code>) (<code>[#498](agronholm/typeguard#498) <https://github.com/agronholm/typeguard/issues/498></code>_)</li> <li>Fixed <code>Self</code> checks in instance/class methods that have positional-only arguments</li> <li>Fixed explicit checks of PEP 604 unions against <code>types.UnionType</code> (<code>[#467](agronholm/typeguard#467) <https://github.com/agronholm/typeguard/issues/467></code>_)</li> <li>Fixed checks against annotations wrapped in <code>NotRequired</code> not being run unless the <code>NotRequired</code> is a forward reference (<code>[#454](agronholm/typeguard#454) <https://github.com/agronholm/typeguard/issues/454></code>_)</li> </ul> <p><strong>4.4.0</strong> (2024-10-27)</p> <ul> <li>Added proper checking for method signatures in protocol checks (<code>[#465](agronholm/typeguard#465) <https://github.com/agronholm/typeguard/pull/465></code>_)</li> <li>Fixed basic support for intersection protocols (<code>[#490](agronholm/typeguard#490) <https://github.com/agronholm/typeguard/pull/490></code>_; PR by <a href="https://github.com/antonagestam"><code>@antonagestam</code></a>)</li> <li>Fixed protocol checks running against the class of an instance and not the instance itself (this produced wrong results for non-method member checks)</li> </ul> <p><strong>4.3.0</strong> (2024-05-27)</p> <ul> <li>Added support for checking against static protocols</li> <li>Fixed some compatibility problems when running on Python 3.13 (<code>[#460](agronholm/typeguard#460) <https://github.com/agronholm/typeguard/issues/460></code>_; PR by <a href="https://github.com/JelleZijlstra"><code>@JelleZijlstra</code></a>)</li> <li>Fixed test suite incompatibility with pytest 8.2 (<code>[#461](agronholm/typeguard#461) <https://github.com/agronholm/typeguard/issues/461></code>_)</li> <li>Fixed pytest plugin crashing on pytest version older than v7.0.0 (even if it's just present) (<code>[#343](agronholm/typeguard#343) <https://github.com/agronholm/typeguard/issues/343></code>_)</li> </ul> <p><strong>4.2.1</strong> (2023-03-24)</p> <ul> <li>Fixed missing <code>typing_extensions</code> dependency for Python 3.12 (<code>[#444](agronholm/typeguard#444) <https://github.com/agronholm/typeguard/issues/444></code>_)</li> <li>Fixed deprecation warning in the test suite on Python 3.13 (<code>[#444](agronholm/typeguard#444) <https://github.com/agronholm/typeguard/issues/444></code>_)</li> </ul> <!-- raw HTML omitted --> </blockquote> <p>... (truncated)</p> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/agronholm/typeguard/commit/cb42103a35abdfce2737d4eb58ed3d0e7f067b4a"><code>cb42103</code></a> Added release date</li> <li><a href="https://github.com/agronholm/typeguard/commit/b32af0bffb52c0b80f2c781656fb67ae1de87947"><code>b32af0b</code></a> Added tests against an empty protocol</li> <li><a href="https://github.com/agronholm/typeguard/commit/1c53caac1c98e33b113463149c01d835730a814f"><code>1c53caa</code></a> Fixed <code>pytest_ignore_collect</code> not to block default pytest code (<a href="https://github.com/agronholm/typeguard/issues/464">#464</a>)</li> <li><a href="https://github.com/agronholm/typeguard/commit/6d6b4de9940071eacbd83430b3d42207a2ac49ff"><code>6d6b4de</code></a> Fixed test_raw_uniontype_fail failing on PyPy</li> <li><a href="https://github.com/agronholm/typeguard/commit/05a677213cd6cc1f6efac4bc5ed1efcbba67da39"><code>05a6772</code></a> Fixed checks being skipped for non-forward-reference NotRequired</li> <li><a href="https://github.com/agronholm/typeguard/commit/7ff5a51d368b6d23d73c11f8f6da8d64b0c55f3a"><code>7ff5a51</code></a> Fixed issue links in the changelog</li> <li><a href="https://github.com/agronholm/typeguard/commit/b0b20c217331e299c20ab743fbb3dfa4c5eef257"><code>b0b20c2</code></a> Fixed explicit checks of PEP 604 unions against <code>types.UnionType</code></li> <li><a href="https://github.com/agronholm/typeguard/commit/0a3983039b6f8e0228ba39cb3c682c565ade19d8"><code>0a39830</code></a> Fixed <code>Self</code> checks when a method has positional-only arguments</li> <li><a href="https://github.com/agronholm/typeguard/commit/eb1e869e2c650db3d1f60a052f1426b52d3fb3cf"><code>eb1e869</code></a> Fixed reference cycles (<a href="https://github.com/agronholm/typeguard/issues/493">#493</a>)</li> <li><a href="https://github.com/agronholm/typeguard/commit/28dafeca1b39b8c19217ce09536e349c94734fa8"><code>28dafec</code></a> Fixed checking of protocols on the class level</li> <li>Additional commits viewable in <a href="https://github.com/agronholm/typeguard/compare/4.4.0...4.4.1">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=typeguard&package-manager=pip&previous-version=4.4.0&new-version=4.4.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Things to check first
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
Typeguard version
4.3.0
Python version
3.10
What happened?
When trying to check if an object is of the type
UnionType
, typeguard rejects regardless of whether the type matches.It is important to note that I am not checking if an object is one of multiple types (using, for example does
"hello"
matchint | str
), I am looking at whether an object is aUnionType
(for example, ifint | str
is aUnionType
). See the MWE for more information and to make clearer what my code is doing.When using the
|
operator when defining type-hints, it will either generate aUnion
orUnionType
type object. If the two objects you are combining are standardtypes
, then it will createUnionType
but if they are complex constructs (such as aLiteral
) then it will create aUnion
object. I am not sure if it is as cut-and-dry as this, but this seems to be the general behaviour.These are handled in
typeguard
by thecheck_union()
andcheck_uniontype()
functions which are currently identical. These function looks through all of the arguments of the provided type and if one of them matches (dispatched tocheck_type_internal()
) then it returns and is considered to have passed. However in my code, I am usingUnionType
on its own without any arguments and so this loop is never entered and thecheck_uniontype()
function always raises aTypeCheckError
.From my understanding the only time a
UnionType
object will be in use without arguments is when it is being used explicitly, and otherwise it will have been created via a|
operation between twotypes
. I also believe that aUnion
would always have arguments as this aSpecialForm
and so other checkers such asmypy
would already fail in this instance and if your function is to take a generalUnion
construct, then it would have to take aSpecialForm
and check the subclassing because otherwise you would need to useUnion[X,Y]
.For this reason, I think a solution would be to include an early escape within the
check_uniontype()
function if theargs
is an empty list and if thevalue
is explicitly aUnionType
. I appreciate if further consideration is needed since the difference betweenUnion
andUnionType
is probably more nuanced than I am assuming.How can we reproduce the bug?
The following code demonstrates the issue (and hopefully will provide some insight into what I am trying to check).
A smaller example would be the following which calls the
check_type()
function directly:The text was updated successfully, but these errors were encountered: