Skip to content

Unable to correctly loading the methods from relatively imported classes #365

Closed

Description

@kunalvishwasrao

Description:
After merging #360 all the previous issues regarding relatively importing step implementations from another project were fixed but when I created step implementations inside a class in different project and tried use those steps inside specs by using the relative import feature, I am getting an error saying missing 1 required positional argument despite of having everything correctly present.

Gauge-Main-Project/step_impl/step_impl.py

from getgauge.python import step, Messages

class BaseMainClass:
    def __init__(self) -> None:
        pass

class MainClass(BaseMainClass):
    @step("Greet <name> from Main Project")
    def greet_main_project(self, name):
        print(f"Hello from the Main Project, {name}")
        Messages.write_message(f"Hello from the Main Project - {name}")

Gauge-Sub-Project/step_impl/step_impl.py

from getgauge.python import step, Messages

class BaseSubClass:
    def __init__(self) -> None:
        pass

class SubClass(BaseSubClass):
    @step("Greet <name> from Sub Project")
    def greet_sub_project(sub, name):
        print(f"Hello from the Sub Project, {name}")
        Messages.write_message(f"Hello from the Main Project - {name}")

Sample Spec File

# Test Relative Imports
> Sample spec from Sub Project

## Test Relative Imports From Class

* Greet "Kunal" from Sub Project
* Greet "Kunal" from Main Project

To Reproduce:
Steps (or project) to reproduce the behavior:

  1. Initialize 2 gauge projects with gauge init python command
  2. Update the step_impl.py files for both the projects like shown above
  3. Setup the Gauge-Sub-Project/env/default/python.properties file like: STEP_IMPL_DIR = step_impl, ../Gauge-Main-Project/step_impl
  4. Create a spec file with the above mentioned steps
  5. Run the spec
  6. See error

Logs

Traceback (most recent call last):
  File "<PATH_TO_VENV>\Lib\site-packages\getgauge\executor.py", line 33, in execute_method
    step.impl(*params)
TypeError: MainClass.greet_main_project() missing 1 required positional argument: 'name'

Possible Fix:
This issue arises because the update_step_resgistry_with_class() method inside the impl_loader.py file is unable to fetch all the methods inside that step implementation file inside get_all_methods_in() method due to the file_path being a relative file path.
Resolving this relative file path into an absolute file path fixes this issue.

Versions:

  • OS - Windows 10 (10.0.19045)
  • Python version - 3.11.4
  • Gauge version - 1.6.7

Project Structure:

Project-Root
  ├── Gauge-Main-Project
  │    ├── env
  │    │   └── default
  │    │       └── default.properties
  │    │       └── python.properties
  │    ├── specs
  │    │   └── example.spec
  │    └── step_impl
  │        └── step_impl.py
  │
  └── Gauge-Sub-Project
      ├── env
      │   └── default
      │       └── default.properties
      │       └── python.properties
      ├── specs
      │   └── example.spec
      └── step_impl
          └── step_impl.py

Thanks!😄

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions