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

subscriptable dicts not recognized as Python 3.9 #86

Closed
barsnick opened this issue Aug 17, 2022 · 5 comments
Closed

subscriptable dicts not recognized as Python 3.9 #86

barsnick opened this issue Aug 17, 2022 · 5 comments

Comments

@barsnick
Copy link

Describe the bug
In Python < 3.9, subscripting a dict results in a type error. This was apparently changed with PEP 585. vermin does not recognize this version dependency.

To Reproduce
Use this file testcase.py:

import typing

bla: dict[int, str] = { 2, "foo", 3, "bar" }
bla2: typing.Dict[int, str] = { 2, "foo", 3, "bar" }

Run as such:

$ vermin.py -vv testcase.py
Detecting python files..
Analyzing using 2 processes..
!2, 3.6      /path/to/testcase.py
  'typing' module requires !2, 3.5
  variable annotations requires !2, 3.6

Tips:
- You're using potentially backported modules: typing
  If so, try using the following for better results: --backport typing
- Since '# novm' or '# novermin' weren't used, a speedup can be achieved using: --no-parse-comments
(disable using: --no-tips)

Minimum required versions: 3.6
Incompatible versions:     2

Expected behavior
Something like this:

!2, 3.9      /path/to/testcase.py
  subscripting dicts requires !2, 3.9

Environment (please complete the following information):
vermin 1.4.1, git ec1e551

Additional context
Note: In order to restore compatibility with Python < 3.9, the subscripted dict can be replaced with typing.Dict, perhaps with an explicit typecast to a list, as used in the example above. That's not what vermin is concerned with, but what I had to do, to get rid of the surprising (as unreported) dependency.

@netromdk
Copy link
Owner

Thanks for reporting this! I'll have a look soon.

@netromdk
Copy link
Owner

As per the caveats section of the README:

Function and variable annotations aren't evaluated at definition time when from __future__ import annotations is used (PEP 563). This is why --no-eval-annotations is on by default (since v1.1.1, #66). If annotations are being evaluated at runtime, like using typing.get_type_hints or evaluating __annotations__ of an object, --eval-annotations should be used for best results.

If you use --eval-annotations then it gives the desired result:

!2, 3.9      issue-86.py
  'typing' module requires !2, 3.5
  builtin generic type annotation (dict[..]) requires !2, 3.9
  set literals requires 2.7, 3.0
  variable annotations requires !2, 3.6

Minimum required versions: 3.9
Incompatible versions:     2

As a side note, I've added heuristics for displaying when generic or literal annotations might be in use, which would display the following tip:

Tips:
- Generic or literal annotations might be in use. If so, try using: --eval-annotations
  But check the caveat section: https://github.com/netromdk/vermin#caveats
(disable using: --no-tips)

But it does not show in this specific case. I'll see if that can happen without ruining things/tests.

@netromdk
Copy link
Owner

netromdk commented Aug 20, 2022

I've improved the heuristics such that it will show the above tip in your case: v1.4.2

@barsnick
Copy link
Author

Great, thanks for the hint! I obviously didn't read the caveats section correctly, sorry.

The added tip is indeed helpful.

@netromdk
Copy link
Owner

Don't mention it. Glad it helped. :-)

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

No branches or pull requests

2 participants