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

@typechecked failed to identify correct type #296

Closed
2 tasks done
FFFluoride opened this issue Feb 18, 2023 · 8 comments
Closed
2 tasks done

@typechecked failed to identify correct type #296

FFFluoride opened this issue Feb 18, 2023 · 8 comments

Comments

@FFFluoride
Copy link

FFFluoride commented Feb 18, 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

2.13.3

Python version

Python 3.11.0rc1

What happened?

What happened
relevant imports:

from typeguard import typechecked
from dataclasses import dataclass

I had a class:

@typechecked
@dataclass
class SpeciesCollection:
  """Class to keep track of species in db"""
  collection: list[dict[float]]

SpeciesCollection was used in class Context:

@typechecked
@dataclass
class Context:
  path: str
  species_dict: SpeciesCollection
  cat_collection: CatCollection

In a different script I imported this module (which is called cat_db_mod_rewrite):

#testing.py
import sys
sys.path.append("../modules/")
from cat_db_mod_rewrite import *
from datetime import datetime

context = Context(
  "~/.pickles",
  SpeciesCollection([
    {"maincoon": 500.0},
    {"black": 400000.0}
  ]),
  CatCollection([])
)

When I ran testing.py, it gave me the error:

Traceback (most recent call last):
  File "/home/fffluoride/dox/python/school/CAT-MAN/programs/testing.py", line 8, in <module>
    SpeciesCollection([
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 1032, in wrapper
    check_argument_types(memo)
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 873, in check_argument_types
    check_type(description, value, expected_type, memo)
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 757, in check_type
    checker_func(argname, value, expected_type, memo)
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 458, in check_list
    check_type('{}[{}]'.format(argname, i), v, value_type, memo)
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 757, in check_type
    checker_func(argname, value, expected_type, memo)
  File "/home/fffluoride/.local/lib/python3.11/site-packages/typeguard/__init__.py", line 416, in check_dict
    key_type, value_type = expected_type.__args__
    ^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

I expected the stack trace to be not that good so I ran this program through the pudb debugger.
I inspected the types and was confused by expected_type being GenericAlias which every time I tried to see what was in the GenericAlias, there was just another GenericAlias. I ran in the debugger shell with the types in the program: check_type(expected_type, value, 'arg') and there was no exception thrown (I have an image attatched)
Screenshot from 2023-02-18 12-39-25.
I don't really understand how exactly this module works so I was wondering if it was a bug.

How can we reproduce the bug?

I made a small script that reproduces the behaviour I saw:

from typeguard import typechecked
from dataclasses import dataclass

@typechecked
@dataclass
class Collection:
    collection: list[dict[float]]

my_collection = Collection([
    {"something": 10},
    {"a_thing": 100}
])
@FFFluoride FFFluoride added the bug label Feb 18, 2023
@FFFluoride
Copy link
Author

Forgot to include the screenshot of pudb for the reproducable version of the issue
Screenshot from 2023-02-18 12-51-31

@agronholm
Copy link
Owner

Have you tried 3.0.0b2 or master? I don' think Typeguard supports Python 3.11 in the 2.x series, and in master, @typechecked has been totally refactored.

@FFFluoride
Copy link
Author

Can I upgrade the package using pip? I'm gonna try to see how I do that.

@FFFluoride
Copy link
Author

FFFluoride commented Feb 18, 2023

Have you tried 3.0.0b2 or master? I don' think Typeguard supports Python 3.11 in the 2.x series, and in master, @typechecked has been totally refactored.

Seems like that didn't work
Screenshot from 2023-02-18 15-29-34

Screen shot on pudb to prove it
Screenshot from 2023-02-18 15-33-17

@agronholm
Copy link
Owner

Sorry, I should've read your issue report better (but I'm on vacation, so that's my excuse). Type checking of class members isn't supported at the moment. Are you sure you checked the existing issues? Like this one?

@FFFluoride
Copy link
Author

oops sorry, I remember scanning through the issues but I must've missed this one. I suppose you can close this issue but may I ask why?

@agronholm
Copy link
Owner

Why checking of class members is not supported? One, I haven't had the time to do it, and two, doing it safely is very tricky.

@agronholm agronholm closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2023
@agronholm agronholm added duplicate and removed bug labels Feb 18, 2023
@FFFluoride
Copy link
Author

I see well good luck with that loving your module despite the support for dataclasses. And hey maybe I'll even contribute If I have the time, enjoy the rest of your holiday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants