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

Using NotRequired disables Literal typechecks #454

Closed
2 tasks done
ionelmc opened this issue Apr 17, 2024 · 1 comment
Closed
2 tasks done

Using NotRequired disables Literal typechecks #454

ionelmc opened this issue Apr 17, 2024 · 1 comment
Labels

Comments

@ionelmc
Copy link

ionelmc commented Apr 17, 2024

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.2.1

Python version

3.11.8

What happened?

Python 3.11.8 (main, Feb 28 2024, 00:00:00) [GCC 13.2.1 20231011 (Red Hat 13.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> class StuffOptional(typing.TypedDict):
...   foo: typing.NotRequired[typing.Literal['bar']]
...
>>> check_type({'foo': 'caca'}, StuffOptional)
{'foo': 'caca'}
>>>

The expected result would be exactly like this:

>>> class Stuff(typing.TypedDict):
...   foo: typing.Literal['bar']
...
>>> from typeguard import check_type
>>> check_type({'foo': 'caca'}, Stuff)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_functions.py", line 106, in check_type
    check_type_internal(value, expected_type, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 779, in check_type_internal
    checker(value, origin_type, args, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 284, in check_typed_dict
    check_type_internal(argvalue, argtype, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 779, in check_type_internal
    checker(value, origin_type, args, memo)
  File ".tox/py311/lib/python3.11/site-packages/typeguard/_checkers.py", line 587, in check_literal
    raise TypeCheckError(f"is not any of ({formatted_args})") from None
typeguard.TypeCheckError: value of key 'foo' of dict is not any of ('bar')

How can we reproduce the bug?

^

@ionelmc ionelmc added the bug label Apr 17, 2024
@ldd-cat
Copy link

ldd-cat commented Oct 24, 2024

I think this is the cause

if isinstance(annotation, ForwardRef):
annotation = evaluate_forwardref(annotation, memo)
if get_origin(annotation) is NotRequired:
required_keys.discard(key)
annotation = get_args(annotation)[0]

this make the check_typed_dict only check for NotRequired field only if it's a string annotation eg a: "NotRequired[int]"

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
&lt;https://semver.org/#semantic-versioning-200&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/486&gt;</code>_)</li>
<li>Fixed <code>TypeError</code> when checking a class against
<code>type[Self]</code>
(<code>[#481](agronholm/typeguard#481)
&lt;https://github.com/agronholm/typeguard/issues/481&gt;</code>_)</li>
<li>Fixed checking of protocols on the class level (against
<code>type[SomeProtocol]</code>)
(<code>[#498](agronholm/typeguard#498)
&lt;https://github.com/agronholm/typeguard/issues/498&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/467&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/454&gt;</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)
&lt;https://github.com/agronholm/typeguard/pull/465&gt;</code>_)</li>
<li>Fixed basic support for intersection protocols
(<code>[#490](agronholm/typeguard#490)
&lt;https://github.com/agronholm/typeguard/pull/490&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/460&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/461&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/343&gt;</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)
&lt;https://github.com/agronholm/typeguard/issues/444&gt;</code>_)</li>
<li>Fixed deprecation warning in the test suite on Python 3.13
(<code>[#444](agronholm/typeguard#444)
&lt;https://github.com/agronholm/typeguard/issues/444&gt;</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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants