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

TypeError raised if type variable is defined before a locust class #3007

Open
2 tasks done
daara-s opened this issue Dec 6, 2024 · 0 comments
Open
2 tasks done

TypeError raised if type variable is defined before a locust class #3007

daara-s opened this issue Dec 6, 2024 · 0 comments
Labels

Comments

@daara-s
Copy link

daara-s commented Dec 6, 2024

Prerequisites

Description

I found a bug which raises the stack-trace below when defining a type variable before a locust class.
It looks to only affect when setting a type as shown in the MWE, though I've not tested many variations.

Removing RequestDict = dict[str, object] fixed the issue.

✦ ❯ locust -f scripts/locustfile.py
Traceback (most recent call last):
  File "/Users/daarashaw/dev/repo/.venv/bin/locust", line 8, in <module>
    sys.exit(main())
  File "/Users/daarashaw/dev/repo/.venv/lib/python3.10/site-packages/locust/main.py", line 112, in main
    docstring, _user_classes, shape_classes = load_locustfile(_locustfile)
  File "/Users/daarashaw/dev/repo/.venv/lib/python3.10/site-packages/locust/util/load_locustfile.py", line 83, in load_locustfile
    shape_classes = [value() for value in vars(imported).values() if is_shape_class(value)]
  File "/Users/daarashaw/dev/repo/.venv/lib/python3.10/site-packages/locust/util/load_locustfile.py", line 83, in <listcomp>
    shape_classes = [value() for value in vars(imported).values() if is_shape_class(value)]
  File "/Users/daarashaw/dev/repo/.venv/lib/python3.10/site-packages/locust/util/load_locustfile.py", line 24, in is_shape_class
    return bool(inspect.isclass(item) and issubclass(item, LoadTestShape) and not getattr(item, "abstract", True))
  File "/Users/daarashaw/.pyenv/versions/3.10.13/lib/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

Command line

locust -f scripts/locustfile.py

Locustfile contents

import json
import random
from pathlib import Path

from locust import HttpUser, between, task
from typing_extensions import override

RequestDict = dict[str, object]


class RequestGenerator(HttpUser):
    host = "http://some_endpoint"

    wait_time = between(1, 3)  # type: ignore[no-untyped-call]

    @override
    def on_start(self) -> None:
        self.requests: list[RequestDict] = self.load_requests()

    def load_requests(self) -> list[RequestDict]:
        json_files = (Path(__file__).parents[1] / "tests" / "jsons").glob("*.json")
        return [json.loads(file.read_text()) for file in json_files]

    @task
    def calculate(self) -> None:
        request = random.choice(self.requests)

        self.client.post(
            "/calculate",
            json=request,
        )

Python version

3.10.13

Locust version

2.32.4

Operating system

MacOS 14.6.1 (23G93)

@daara-s daara-s added the bug label Dec 6, 2024
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

1 participant