All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: Max Reitz <mreitz@redhat.com>, qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Kevin Wolf <kwolf@redhat.com>
Subject: Re: [PATCH v3 3/4] migrate-bitmaps-test: Fix pylint warnings
Date: Fri, 14 May 2021 19:40:04 +0300	[thread overview]
Message-ID: <dd0c6b92-d081-1e30-ba7a-8b4240b9a62a@virtuozzo.com> (raw)
In-Reply-To: <20210514154351.629027-4-mreitz@redhat.com>

14.05.2021 18:43, Max Reitz wrote:
> There are a couple of things pylint takes issue with:
> - The "time" import is unused
> - The import order (iotests should come last)
> - get_bitmap_hash() doesn't use @self and so should be a function
> - Semicolons at the end of some lines
> - Parentheses after "if"
> - Some lines are too long (80 characters instead of 79)
> - inject_test_case()'s @name parameter shadows a top-level @name
>    variable
> - "lambda self: mc(self)" were equivalent to just "mc", but in
>    inject_test_case(), it is not equivalent, so add a comment and disable
>    the warning locally
> - Always put two empty lines after a function
> - f'exec: cat > /dev/null' does not need to be an f-string
> 
> Fix them.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---

[..]

> -def inject_test_case(klass, name, method, *args, **kwargs):
> +def inject_test_case(klass, suffix, method, *args, **kwargs):
>       mc = operator.methodcaller(method, *args, **kwargs)
> -    setattr(klass, 'test_' + method + name, lambda self: mc(self))
> +    # The lambda is required to enforce the `self` parameter.  Without it,
> +    # `mc` would be called without any arguments, and then complain.
> +    # pylint: disable=unnecessary-lambda
> +    setattr(klass, 'test_' + method + suffix, lambda self: mc(self))
> +
>   

Interesting... I decided to experiment a bit, and what I can say now:

The actual reason is that class attrubute of type <class 'function'>, becomes a <class 'method'> of the class instance on instantiation.

lambda is a function, so on instantiation we'll have "method", and method can be called as obj.method(), and original function will get "self" first argument automatically.

mc is not a function, it's <class 'operator.methodcaller'>, so there is no magic, instance of the class doesn't get own method but just a refence to class variable instead.

So, let's modify comment to something like:

We want to add function attribute to class, so that it correctly converted to method on instantiation. lamba is necessary to "convert" methodcaller object (which is callable, but not a function) to function.


with it:
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

==== my expirements =====

# cat x.py
import operator

class X:
     def hello(self, arg):
         print("hello", arg)


mc = operator.methodcaller("hello", "Vova")
lmd = lambda self: mc(self)

print('mc:', type(mc))
print('lmd:', type(lmd))

setattr(X, "test_hello_direct", mc)
setattr(X, "test_hello_lambda", lmd)
X.simply_assigned = lmd

x = X()

x.assigned_to_instance = lmd

print('mc attached:', type(x.test_hello_direct))
print('lmd attached:', type(x.test_hello_lambda))
print('lmd simply assigned:', type(x.simply_assigned))
print('lmd assigned to instance:', type(x.assigned_to_instance))

x.test_hello_lambda()
x.simply_assigned()

print("x.test_hello_lambda is x.simply_assigned", x.test_hello_lambda is x.simply_assigned)
print("x.test_hello_lambda is X.test_hello_lambda", x.test_hello_lambda is X.test_hello_lambda)
print("x.test_hello_direct is X.test_hello_direct", x.test_hello_direct is X.test_hello_direct)
print("X.test_hello_lambda is X.simply_assigned", X.test_hello_lambda is X.simply_assigned)
print("X.test_hello_lambda type:", type(X.test_hello_lambda))

try:
     x.assigned_to_instance()
except Exception as e:
     print("assigned to instance call failed:", e)

try:
     x.test_hello_direct()
except Exception as e:
     print("direct call failed:", e)



# python3 x.py
mc: <class 'operator.methodcaller'>
lmd: <class 'function'>
mc attached: <class 'operator.methodcaller'>
lmd attached: <class 'method'>
lmd simply assigned: <class 'method'>
lmd assigned to instance: <class 'function'>
hello Vova
hello Vova
x.test_hello_lambda is x.simply_assigned False
x.test_hello_lambda is X.test_hello_lambda False
x.test_hello_direct is X.test_hello_direct True
X.test_hello_lambda is X.simply_assigned True
X.test_hello_lambda type: <class 'function'>
assigned to instance call failed: <lambda>() missing 1 required positional argument: 'self'
direct call failed: methodcaller expected 1 argument, got 0


-- 
Best regards,
Vladimir


  reply	other threads:[~2021-05-14 16:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-14 15:43 [PATCH v3 0/4] iotests/297: Cover tests/ Max Reitz
2021-05-14 15:43 ` [PATCH v3 1/4] iotests/297: Drop 169 and 199 from the skip list Max Reitz
2021-05-14 15:43 ` [PATCH v3 2/4] migrate-bitmaps-postcopy-test: Fix pylint warnings Max Reitz
2021-05-14 15:43 ` [PATCH v3 3/4] migrate-bitmaps-test: " Max Reitz
2021-05-14 16:40   ` Vladimir Sementsov-Ogievskiy [this message]
2021-05-14 15:43 ` [PATCH v3 4/4] iotests/297: Cover tests/ Max Reitz
2021-09-01 13:34 ` [PATCH v3 0/4] " Vladimir Sementsov-Ogievskiy
2021-09-01 14:33   ` Hanna Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dd0c6b92-d081-1e30-ba7a-8b4240b9a62a@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.