allure-python icon indicating copy to clipboard operation
allure-python copied to clipboard

@allure.step decorator fails if one of the method parameters has a default value

Open tanistra opened this issue 7 years ago • 5 comments

I have a method that has the default argument set to "None". (it can be string as well)

@allure.step(
    "Send message: '{message}' on {channel} with props: '{props}'")
def send_message(
        self,
        channel: str,
        message: str,
        username: str = None,
        props: Dict = None) -> None:
    ...

Calling this method without the props parameter:

self.bot.send_message(
       channel=self.DZIUNIA['mm_channel_id'],
       message=letter)

causes a Python exception:

E KeyError: 'props'

tanistra avatar Oct 31 '19 17:10 tanistra

I have seen this too. I assume the hook doesn't get a 'props' in this case, therefore fails. Yet, the function would be the next step after the hook, where it forces props = None making it a known variable.

Not sure if it's a bug, or working as intended, due to order of operations. To that end, my workaround would be using a context manager for the whole step:

def send_message(
        self,
        channel: str,
        message: str,
        username: str = None,
        props: Dict = None) -> None:
     with allure.step(
        "Send message: '{message}' on {channel} with props: '{props}'")
        <code>

Hope that helps for the mean time!

mlewandowskipear avatar Oct 31 '19 19:10 mlewandowskipear

I don't know if it's a bug either.

I will try to replace decorator by your solution.

Thanks!

tanistra avatar Oct 31 '19 20:10 tanistra

image The problem is here - in this case only kwargs will be treated as a parameters But actually we need to take all default parameters from function object and merge them with kwargs

Is there any updates? Please note that the issue occurs on 2.8.15 version as well

Araksya-Hambaryan avatar May 25 '20 08:05 Araksya-Hambaryan

Is there any updates? Please note that the issue occurs on 2.8.15 version as well

PR https://github.com/allure-framework/allure-python/pull/569

Denis-Alexeev avatar Mar 03 '21 13:03 Denis-Alexeev