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

Literal checking discrepancy between typing vs typing-extensions 3.9 #363

Closed
2 tasks done
hmc-cs-mdrissi opened this issue May 26, 2023 · 0 comments · Fixed by #366
Closed
2 tasks done

Literal checking discrepancy between typing vs typing-extensions 3.9 #363

hmc-cs-mdrissi opened this issue May 26, 2023 · 0 comments · Fixed by #366
Labels

Comments

@hmc-cs-mdrissi
Copy link

hmc-cs-mdrissi commented May 26, 2023

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

Python version

3.9.16

What happened?

>>> from typeguard import check_type
>>> from typing import Literal
>>> check_type(42, Literal["hello"])
42
>>> from typing_extensions import Literal
>>> check_type(42, Literal["hello"])
typeguard.TypeCheckError: int is not any of ('hello')

How can we reproduce the bug?

It looks like typing_extensions.Literal works fine, but typing.Literal in 3.9 checking simple int on Literal["hello"] passes incorrectly. typing extensions version I have is 4.6. My guess is issue is related to here. Unsure why Literal is imported in 3.11 from typing, but both typing.Literal and typing_extensions.Literal need to be in origin_type_checkers. Or some more type normalization needs to happen.

My guess is other imports there like LiteralString may have similar bug.

charliermarsh pushed a commit to astral-sh/ruff-lsp that referenced this issue Jun 16, 2023
Bumps [typeguard](https://github.com/agronholm/typeguard) from 3.0.2 to
4.0.0.
<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>UNRELEASED</strong></p>
<ul>
<li>Fixed handling of <code>typing_extensions.Literal</code> on Python
3.8 and 3.9 when
<code>typing_extensions&gt;=4.6.0</code> is installed
(<code>[#363](agronholm/typeguard#363)
&lt;https://github.com/agronholm/typeguard/issues/363&gt;</code>_; PR by
Alex Waygood)</li>
<li>Fixed <code>NameError</code> when generated type checking code
references an imported name from
a method
(<code>[#362](agronholm/typeguard#362)
&lt;https://github.com/agronholm/typeguard/issues/362&gt;</code>_)</li>
<li>Fixed docstrings disappearing from instrumented functions
(<code>[#359](agronholm/typeguard#359)
&lt;https://github.com/agronholm/typeguard/issues/359&gt;</code>_)</li>
</ul>
<p><strong>4.0.0</strong> (2023-05-12)</p>
<ul>
<li>No changes</li>
</ul>
<p><strong>4.0.0rc6</strong> (2023-05-07)</p>
<ul>
<li>Fixed <code>@TypeChecked</code> optimization causing compilation of
instrumented code to fail
when an <code>if</code> block was left empty by the AST transformer
(<code>[#352](agronholm/typeguard#352)
&lt;https://github.com/agronholm/typeguard/issues/352&gt;</code>_)</li>
<li>Fixed the AST transformer trying to parse the second argument of
<code>typing.Annotated</code>
as a forward reference
(<code>[#353](agronholm/typeguard#353)
&lt;https://github.com/agronholm/typeguard/issues/353&gt;</code>_)</li>
</ul>
<p><strong>4.0.0rc5</strong> (2023-05-01)</p>
<ul>
<li>Added <code>InstrumentationWarning</code> to the public API</li>
<li>Changed <code>@TypeChecked</code> to skip instrumentation in
optimized mode, as in typeguard
2.x</li>
<li>Avoid type checks where the types in question are shadowed by local
variables</li>
<li>Fixed instrumentation using <code>typing.Optional</code> without a
subscript when the subscript
value was erased due to being an ignored import</li>
<li>Fixed <code>TypeError: isinstance() arg 2 must be a type or tuple of
types</code> when
instrumented code tries to check a value against a naked
(<code>str</code>, not <code>ForwardRef</code>)
forward reference</li>
<li>Fixed instrumentation using the wrong &quot;self&quot; type in the
<code>__new__()</code> method</li>
</ul>
<p><strong>4.0.0rc4</strong> (2023-04-15)</p>
<ul>
<li>Fixed imports guarded by <code>if TYPE_CHECKING:</code> when used
with subscripts
(<code>SomeType[...]</code>) being replaced with <code>Any[...]</code>
instead of just <code>Any</code></li>
<li>Fixed instrumentation inadvertently mutating a function's
annotations on Python 3.7
and 3.8</li>
<li>Fixed <code>Concatenate[...]</code> in <code>Callable</code>
parameters causing <code>TypeError</code> to be
raised</li>
<li>Fixed type checks for <code>*args</code> or <code>**kwargs</code>
not being suppressed when their types
are unusable (guarded by <code>if TYPE_CHECKING:</code> or
otherwise)</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/agronholm/typeguard/commit/887e27e033ad1adbba009ab7d991a0af985a7290"><code>887e27e</code></a>
Declared the latest RC as final</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/668d2a02bfea49bbf4f0849c6d050ce731843a71"><code>668d2a0</code></a>
Added release date</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/be4dd3350f15ec8b5713e40955dad3f4c9a68ad6"><code>be4dd33</code></a>
Don't try to parse the second argument of Annotated as a forward
reference</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/3863c0f494c697e52f124f231a18fc2c172d9538"><code>3863c0f</code></a>
Fixed AST transformations potentially leaving &quot;if&quot; bodies
empty</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/a2090ed43c05d514707f2abbe5fb48a4ce144f26"><code>a2090ed</code></a>
[pre-commit.ci] pre-commit autoupdate (<a
href="https://redirect.github.com/agronholm/typeguard/issues/351">#351</a>)</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/a6a9b715a753decf120a1bb571815165daa7ebf4"><code>a6a9b71</code></a>
Use the proper environment for release</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/c888515f830ccb4d3f088b44b60bbe105f81d1e6"><code>c888515</code></a>
Added release date</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/8923b7b40e03ebdd874a543e17de9bd865ea9ada"><code>8923b7b</code></a>
Made <code>@TypeChecked</code> a no-op in optimized mode (<a
href="https://redirect.github.com/agronholm/typeguard/issues/350">#350</a>)</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/43d686e7306aa285ac8fc94bea45923a354bb7c6"><code>43d686e</code></a>
Switch to the official coveralls action</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/e688da2dc817dc66d042e4f7381d3dc25ee19bcb"><code>e688da2</code></a>
Skip type checks where the type names are shadowed by local
variables</li>
<li>Additional commits viewable in <a
href="https://github.com/agronholm/typeguard/compare/3.0.2...4.0.0">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=3.0.2&new-version=4.0.0)](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 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>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
zanieb pushed a commit to astral-sh/ruff-lsp that referenced this issue Jul 28, 2023
Bumps [typeguard](https://github.com/agronholm/typeguard) from 3.0.2 to
4.0.1.
<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.0.1</strong> (2023-07-27)</p>
<ul>
<li>Fixed handling of <code>typing_extensions.Literal</code> on Python
3.8 and 3.9 when
<code>typing_extensions&gt;=4.6.0</code> is installed
(<code>[#363](agronholm/typeguard#363)
&lt;https://github.com/agronholm/typeguard/issues/363&gt;</code>_; PR by
Alex Waygood)</li>
<li>Fixed <code>NameError</code> when generated type checking code
references an imported name from
a method
(<code>[#362](agronholm/typeguard#362)
&lt;https://github.com/agronholm/typeguard/issues/362&gt;</code>_)</li>
<li>Fixed docstrings disappearing from instrumented functions
(<code>[#359](agronholm/typeguard#359)
&lt;https://github.com/agronholm/typeguard/issues/359&gt;</code>_)</li>
<li>Fixed <code>@TypeChecked</code> failing to instrument functions when
there are more than one
function within the same scope
(<code>[#355](agronholm/typeguard#355)
&lt;https://github.com/agronholm/typeguard/issues/355&gt;</code>_)</li>
<li>Fixed <code>frozenset</code> not being checked
(<code>[#367](agronholm/typeguard#367)
&lt;https://github.com/agronholm/typeguard/issues/367&gt;</code>_)</li>
</ul>
<p><strong>4.0.0</strong> (2023-05-12)</p>
<ul>
<li>No changes</li>
</ul>
<p><strong>4.0.0rc6</strong> (2023-05-07)</p>
<ul>
<li>Fixed <code>@TypeChecked</code> optimization causing compilation of
instrumented code to fail
when an <code>if</code> block was left empty by the AST transformer
(<code>[#352](agronholm/typeguard#352)
&lt;https://github.com/agronholm/typeguard/issues/352&gt;</code>_)</li>
<li>Fixed the AST transformer trying to parse the second argument of
<code>typing.Annotated</code>
as a forward reference
(<code>[#353](agronholm/typeguard#353)
&lt;https://github.com/agronholm/typeguard/issues/353&gt;</code>_)</li>
</ul>
<p><strong>4.0.0rc5</strong> (2023-05-01)</p>
<ul>
<li>Added <code>InstrumentationWarning</code> to the public API</li>
<li>Changed <code>@TypeChecked</code> to skip instrumentation in
optimized mode, as in typeguard
2.x</li>
<li>Avoid type checks where the types in question are shadowed by local
variables</li>
<li>Fixed instrumentation using <code>typing.Optional</code> without a
subscript when the subscript
value was erased due to being an ignored import</li>
<li>Fixed <code>TypeError: isinstance() arg 2 must be a type or tuple of
types</code> when
instrumented code tries to check a value against a naked
(<code>str</code>, not <code>ForwardRef</code>)
forward reference</li>
<li>Fixed instrumentation using the wrong &quot;self&quot; type in the
<code>__new__()</code> method</li>
</ul>
<p><strong>4.0.0rc4</strong> (2023-04-15)</p>
<ul>
<li>Fixed imports guarded by <code>if TYPE_CHECKING:</code> when used
with subscripts
(<code>SomeType[...]</code>) being replaced with <code>Any[...]</code>
instead of just <code>Any</code></li>
<li>Fixed instrumentation inadvertently mutating a function's
annotations on Python 3.7</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/agronholm/typeguard/commit/cc5cc54e733d472fda6f805525908e43465a6d05"><code>cc5cc54</code></a>
Added release date</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/2766aa7f8e48fc8da317480f0c478711e05d8ac4"><code>2766aa7</code></a>
Install typing_extensions on Python 3.11 too</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/00ac62195f6c936de4d19465dababb5b06388685"><code>00ac621</code></a>
Fixed frozenset not being checked</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/686f25a9dcc1bd615ea0283b3c3a698bf09a76ac"><code>686f25a</code></a>
Bumped the minimum version of typing_extensions</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/1950db184e966b06129f545c558daf969ef9580e"><code>1950db1</code></a>
Updated pre-commit modules</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/53525e11a3bb1af74f3bd8b3bb62c41408828fd2"><code>53525e1</code></a>
Fixed <code>@TypeChecked</code> failing to instrument functions with
duplicate names in ...</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/f377be389765ed0db104b41d78fce3c45e72e149"><code>f377be3</code></a>
Fixed deprecation warnings on Python 3.12</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/91204af820c3e849ecebd88a6da818e7164a1a81"><code>91204af</code></a>
Test against Python 3.12</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/05b1c6727a3302c8b25d909122fffa5901756867"><code>05b1c67</code></a>
Fixed docstrings being unavailable after instrumentation</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/845fcbcbfdaed2bca9704c3ed4adacd33f5ec314"><code>845fcbc</code></a>
Fixed NameError when a decorated method references an imported name</li>
<li>Additional commits viewable in <a
href="https://github.com/agronholm/typeguard/compare/3.0.2...4.0.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=3.0.2&new-version=4.0.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 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>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
zanieb pushed a commit to astral-sh/ruff-lsp that referenced this issue Aug 18, 2023
Bumps [typeguard](https://github.com/agronholm/typeguard) from 4.1.1 to
4.1.2.
<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>UNRELEASED</strong></p>
<ul>
<li>Dropped Python 3.7 support</li>
</ul>
<p><strong>4.1.2</strong> (2023-08-18)</p>
<ul>
<li>Fixed <code>Any</code> being removed from a subscript that still
contains other elements
(<code>[#373](agronholm/typeguard#373)
&lt;https://github.com/agronholm/typeguard/issues/373&gt;</code>_</li>
</ul>
<p><strong>4.1.1</strong> (2023-08-16)</p>
<ul>
<li>Fixed <code>suppress_type_checks()</code> causing annotated variable
assignments to always
assign <code>None</code>
(<code>[#380](agronholm/typeguard#380)
&lt;https://github.com/agronholm/typeguard/issues/380&gt;</code>_)</li>
</ul>
<p><strong>4.1.0</strong> (2023-07-30)</p>
<ul>
<li>Added support for passing a tuple as <code>expected_type</code> to
<code>check_type()</code>, making it
more of a drop-in replacement for <code>isinstance()</code>
(<code>[#371](agronholm/typeguard#371)
&lt;https://github.com/agronholm/typeguard/issues/371&gt;</code>_</li>
<li>Fixed regression where <code>Literal</code> inside a
<code>Union</code> had quotes stripped from its
contents, thus typically causing <code>NameError</code> to be raised
when run
(<code>[#372](agronholm/typeguard#372)
&lt;https://github.com/agronholm/typeguard/issues/372&gt;</code>_)</li>
</ul>
<p><strong>4.0.1</strong> (2023-07-27)</p>
<ul>
<li>Fixed handling of <code>typing_extensions.Literal</code> on Python
3.8 and 3.9 when
<code>typing_extensions&gt;=4.6.0</code> is installed
(<code>[#363](agronholm/typeguard#363)
&lt;https://github.com/agronholm/typeguard/issues/363&gt;</code>_; PR by
Alex Waygood)</li>
<li>Fixed <code>NameError</code> when generated type checking code
references an imported name from
a method
(<code>[#362](agronholm/typeguard#362)
&lt;https://github.com/agronholm/typeguard/issues/362&gt;</code>_)</li>
<li>Fixed docstrings disappearing from instrumented functions
(<code>[#359](agronholm/typeguard#359)
&lt;https://github.com/agronholm/typeguard/issues/359&gt;</code>_)</li>
<li>Fixed <code>@TypeChecked</code> failing to instrument functions when
there are more than one
function within the same scope
(<code>[#355](agronholm/typeguard#355)
&lt;https://github.com/agronholm/typeguard/issues/355&gt;</code>_)</li>
<li>Fixed <code>frozenset</code> not being checked
(<code>[#367](agronholm/typeguard#367)
&lt;https://github.com/agronholm/typeguard/issues/367&gt;</code>_)</li>
</ul>
<p><strong>4.0.0</strong> (2023-05-12)</p>
<ul>
<li>No changes</li>
</ul>
<p><strong>4.0.0rc6</strong> (2023-05-07)</p>
<ul>
<li>Fixed <code>@TypeChecked</code> optimization causing compilation of
instrumented code to fail</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/agronholm/typeguard/commit/d7a705e329529456898265d94adbdafed0c4c22c"><code>d7a705e</code></a>
Added release date</li>
<li><a
href="https://github.com/agronholm/typeguard/commit/d07fbb4fd49e716c4db12a6fca3dcfa8a58fd6d2"><code>d07fbb4</code></a>
Fixed Any being removed from a subscript that still contains other
elements</li>
<li>See full diff in <a
href="https://github.com/agronholm/typeguard/compare/4.1.1...4.1.2">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.1.1&new-version=4.1.2)](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>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant