Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

nameof error when running program, but not when running in debug #79

Closed
VincentPelletier1 opened this issue Jun 28, 2022 · 13 comments
Closed
Labels
needs-more-info Not enough information to dig into the issue

Comments

@VincentPelletier1
Copy link

Hi there, I've been developing a code for a couple of days and it worked seemingly well. However, today, I started seeing this error:

"Couldn't retrieve the call node. "
varname.utils.VarnameRetrievingError: Couldn't retrieve the call node. This may happen if you're using some other AST magic at the same time, such as pytest, ipython, macropy, or birdseye.

when running the program. However, when I break just above the problematic line and go step by step, the problem does not occur. I've also checked an no instances of pytest of any kind is running.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

Could you share your piece of functioning code and your running environment (i.e. python version, whether code is running in a python file, REPL or jupyter)?

@VincentPelletier1
Copy link
Author

VincentPelletier1 commented Jun 28, 2022

The code running is unfortunately too big to share, but I can give information about its environnement:
python 3.7.5 is installed with the following env:
PyNaCl | 1.5.0 | 1.5.0
PyVISA | 1.10.1 | 1.12.0
PyYAML | 6.0 | 6.0
asttokens | 2.0.5 | 2.0.5
atomicwrites | 1.4.0 | 1.4.0
attrs | 21.4.0 | 21.4.0
awg | 1.7.0 | 0.1.1
bcrypt | 3.2.2 | 3.2.2
cachy | 0.3.0 | 0.3.0
certifi | 2022.6.15 | 2022.6.15
cffi | 1.15.0 | 1.15.0
charset-normalizer | 2.0.12 | 2.1.0
cleo | 0.8.1 | 0.8.1
clikit | 0.6.2 | 0.6.2
clock-divider | 1.0.0 |  
colorama | 0.4.4 | 0.4.5
communication-interface | 0.2.0a0 |  
coverage | 6.4.1 | 6.4.1
crashtest | 0.3.1 | 0.3.1
cryptography | 37.0.2 | 37.0.3
distlib | 0.3.4 | 0.3.4
docker | 4.4.4 | 5.0.3
docstring-parser | 0.14.1 | 0.14.1
executing | 0.8.3 | 0.8.3
filelock | 3.7.1 | 3.7.1
future | 0.18.2 | 0.18.2
html5lib | 1.1 | 1.1
idna | 3.3 | 3.3
importlib-metadata | 4.11.3 | 4.12.0
importlib-resources | 5.4.0 | 5.8.0
iniconfig | 1.1.1 | 1.1.1
jsonref | 0.2 | 0.2
jsonschema | 4.4.0 | 4.6.1
keyring | 22.3.0 | 23.6.0
le-compatibility-matrix | 1.18.0 |  
le-config-navigator | 0.3.0 |  
le-constants | 1.8.0 |  
le-eval-board | 1.0.5 |  
le-scpi | 1.0.1 |  
le-simulator | 1.0.1 |  
le-test-as | 2.1.3 |  
le-test-frame | 2.3.4 |  
lockfile | 0.12.2 | 0.12.2
mf-io-board | 0.1.4 |  
more-itertools | 8.12.0 | 8.13.0
msgpack | 1.0.4 | 1.0.4
nifpga | 19.0.0 | 20.0.0
numpy | 1.21.1 | 1.23.0
oscilloscope | 1.4.0 | 0.0.4
packaging | 20.9 | 21.3
pandas | 1.1.5 | 1.4.3
paramiko | 2.11.0 | 2.11.0
pastel | 0.2.1 | 0.2.1
pefile | 2021.5.24 | 2022.5.30
pexpect | 4.8.0 | 4.8.0
pip | 22.1.2 | 22.1.2
pkginfo | 1.8.3 | 1.8.3
platformdirs | 2.5.2 | 2.5.2
pluggy | 0.13.1 | 1.0.0
poetry | 1.1.13 | 1.1.13
poetry-core | 1.0.8 | 1.0.8
psutil | 5.9.1 | 5.9.1
ptyprocess | 0.7.0 | 0.7.0
pure-eval | 0.2.2 | 0.2.2
py | 1.11.0 | 1.11.0
pycparser | 2.21 | 2.21
pylev | 1.4.0 | 1.4.0
pyparsing | 2.4.7 | 3.0.9
pyrsistent | 0.18.1 | 0.18.1
pyserial | 3.5 | 3.5
pytest | 5.4.3 | 7.1.2
pytest-check | 0.3.7 |  
pytest-cov | 3.0.0 | 3.0.0
pytest-html | 3.1.1 | 3.1.1
pytest-metadata | 1.11.0 | 2.0.1
pytest-reportlog | 0.1.2 | 0.1.2
python-dateutil | 2.8.2 | 2.8.2
pytz | 2022.1 | 2022.1
pywin32 | 228 | 304
pywin32-ctypes | 0.2.0 | 0.2.0
requests | 2.28.0 | 2.28.0
requests-toolbelt | 0.9.1 | 0.9.1
scipy | 1.6.1 | 1.8.1
sdkwrapper | 3.1.1a0 |  
semver | 2.13.0 | 2.13.0
setuptools | 60.2.0 | 62.6.0
shellingham | 1.4.0 | 1.4.0
six | 1.16.0 | 1.16.0
smu-core | 1.2.1 |  
tomli | 2.0.1 | 2.0.1
tomlkit | 0.11.0 | 0.11.0
types-PyYAML | 6.0.7 | 6.0.9
types-cryptography | 3.3.21 | 3.3.21
types-paramiko | 2.10.0 | 2.11.1
typing-extensions | 4.1.1 | 4.2.0
urllib3 | 1.26.9 | 1.26.9
varname | 0.8.3 | 0.8.3
virtualenv | 20.15.0 | 20.15.0
wcwidth | 0.2.5 | 0.2.5
webencodings | 0.5.1 | 0.5.1
websocket-client | 1.3.3 | 1.3.3
wheel | 0.37.1 | 0.37.1
zhinst | 20.1.2199 | 22.2.29711
zipp | 3.7.0 | 3.8.0

Note that I also invalidated the cache (I'm using pycharm) and it did not fix the problem.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

Is there any chance that this is related to #75 ?

@VincentPelletier1
Copy link
Author

It is the same error, but the behavior I have is a bit more erratic. I've just come back from lunch, restarted the terminal and the error seems to be gone for the moment.

I know Pycharm can have some performance issues at times, so maybe this is what I'm witnessing.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

varname requires the source code to be seen at runtime.

I myself am not a PyCharm user, but I do see people reporting in some cases the code is executed with exec() (where the source code is not seen at runtime).

However, your case is a little wired as the error is not raised consistently.

Let's see if we could gather more info.

@pwwang pwwang added the needs-more-info Not enough information to dig into the issue label Jun 28, 2022
@VincentPelletier1
Copy link
Author

One thing I changed was to remove the nameof() method from my nested loops since this is where the code was stopping. I doubt this is the problem, but maybe it could be related.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

What matters is how it's executed, rather than where it's executed.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

I guess a sketch of your code will give more help.

@VincentPelletier1
Copy link
Author

def meta_tag_replacement(mocked_ileddarspapi: MagicMock) -> MagicMock:
    mocked_ileddarspapi_name = nameof(mocked_ileddarspapi)
    method_to_mock = {
        'buildMetadataTag':
        {
            'buildAttributes': [100],
            'buildNumber': 100,
            'commitId': 100
        }
    }
    method_list = []

    [method_list.append(method) for method in dir(mocked_ileddarspapi.VersionGet_PWrap()[1]) if
     method not in dir(object) if not method.startswith('__') if method not in dir(MagicMock)]

    mocked_ileddarspapi.VersionGet_PWrap.return_value = (OUTPUT_STATUS, MagicMock(mocked_ileddarspapi.VersionGet_PWrap()[1]))

    for method in method_list:
        if method not in dir(object) and not method.startswith('__') and method not in dir(MagicMock) and method in method_to_mock.keys():
            for method_to_change in method_to_mock[method].keys():
                eval(mocked_ileddarspapi_name + '.' + nameof(mocked_ileddarspapi.VersionGet_PWrap) + '.' + method + '.' + method_to_change).return_value = method_to_mock[method][method_to_change]

    return mocked_ileddarspapi

@VincentPelletier1
Copy link
Author

VincentPelletier1 commented Jun 28, 2022

I've left a single nameof() instance inside the double for loop to show where the nameof() was initially.

@pwwang
Copy link
Owner

pwwang commented Jun 28, 2022

  1. Does it make any difference if we save nameof(mocked_ileddarspapi.VersionGet_PWrap) outside the loops:
    # your code
    # ...
    mocked_ileddarspapi_versionget_pwrap = nameof(mocked_ileddarspapi.VersionGet_PWrap)
    for ...
        if ....
            for ...
                eval(mocked_ileddarspapi_name + '.' + mocked_ileddarspapi_versionget_pwrap + '.' + method + '.' + method_to_change).return_value = method_to_mock[method][method_to_change]
  1. If you intended to use nameof() here, then it's not necessary, since nameof(mocked_ileddarspapi) is literally "mocked_ileddarspapi" and nameof(mocked_ileddarspapi.VersionGet_PWrap) is literally "VersionGet_PWrap". You probably meant to use argname()

@VincentPelletier1
Copy link
Author

VincentPelletier1 commented Jun 28, 2022

I tried putting the nameof() out of the loop's context, but it didn't change a thing. However, I found out that starting an instance of pytest at the same time does yield the error I first stated, but even more than that: it seems to latch onto the error even after the pytest program is killed and its terminal deleted.

(I'll try argname! Thanks for the tip) --> still the error remains

@pwwang
Copy link
Owner

pwwang commented Jul 8, 2022

For the pytest issue, see if this helps:

https://stackoverflow.com/questions/47744076/preventing-pytest-from-creating-cache-directories-in-pycharm

Repository owner locked and limited conversation to collaborators Jul 21, 2022
@pwwang pwwang converted this issue into discussion #83 Jul 21, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
needs-more-info Not enough information to dig into the issue
Projects
None yet
Development

No branches or pull requests

2 participants