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

Clean up var names in unit tests, avoid MP API access in GitHub workflow #207

Merged
merged 17 commits into from
Sep 29, 2024

Conversation

DanielYang59
Copy link
Collaborator

@DanielYang59 DanielYang59 commented Sep 25, 2024

Summary

  • Clean up var names in unit tests, make module level var capital and remove a few unused vars
  • Avoid fetching structure from MP database as API key cannot be passed to actions triggered by forks

@DanielYang59 DanielYang59 changed the title Clean up var names in uni tests Clean up var names in unit tests Sep 25, 2024
@DanielYang59 DanielYang59 added testing Test all the things linting Linting & quality assurance labels Sep 25, 2024
@DanielYang59 DanielYang59 self-assigned this Sep 25, 2024
@DanielYang59 DanielYang59 requested a review from janosh September 25, 2024 08:51
@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 25, 2024

I believe I have seen this error in another PR or somewhere:

File "/opt/hostedtoolcache/Python/3.10.15/x64/lib/python3.10/site-packages/mp_api/client/core/client.py", line 1031, in _submit_request_and_process
    raise MPRestError(
mp_api.client.core.client.MPRestError: REST query returned with error status code 401 on URL https://api.materialsproject.org/materials/core/?deprecated=False&_fields=structure&material_ids=mp-19017&_limit=1000 with message:
Response {
  "message":"No API key found in request"
}
Error: Process completed with exit code 1.

Looks like MP API key got lost sometimes?

@janosh
Copy link
Owner

janosh commented Sep 25, 2024

i can't reproduce the error locally. just tried with the latest version mp-api==0.42.2. the repo definitely has the secret

gh secret list
NAME        UPDATED          
MP_API_KEY  about 24 days ago
PYPI_TOKEN  about 1 year ago

and it's passed correctly

MP_API_KEY: ${{ secrets.MP_API_KEY }}

@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 25, 2024

Yep that occurs intermittently, not sure what the reason is. I didn't remember where I saw this last time, would post it here if I find that.

@DanielYang59

This comment was marked as outdated.

@janosh
Copy link
Owner

janosh commented Sep 27, 2024

@DanielYang59 thanks for debugging. agreed that MP_API_KEY should not be read at module import time but during MPRester instance creation. pinging @tsmathis and @esoteric-ephemera in case you want to consider changing

not sure that's the root cause here though. MP_API_KEY should be available before python is invoked

- name: Run script
  run: python ${{ matrix.script }}
  env:
    MP_API_KEY: ${{ secrets.MP_API_KEY }}

@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 27, 2024

agreed that MP_API_KEY should not be read at module import time but during MPRester instance creation.

I might open an issue there for easier tracking

@DanielYang59 DanielYang59 force-pushed the interactive-unit-test branch 2 times, most recently from d011fef to 2c6f994 Compare September 27, 2024 13:10
raise RuntimeError("api key is None")

elif mp_api_key is not None and len(mp_api_key.strip()) == 0:
raise RuntimeError("mp api key is empty")
Copy link
Collaborator Author

@DanielYang59 DanielYang59 Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janosh can you have another look at the MP_API_KEY secret in our repo (I don't have access to that)? It looks like being empty?

RuntimeError: mp api key is empty

Copy link
Owner

@janosh janosh Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't see it either. once you set a secret you can only change it's value, not see the current value. i just reset the secret to what i think it was before so let's see if that makes a difference

Copy link
Collaborator Author

@DanielYang59 DanielYang59 Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still no luck. I'm not seeing the test.yml or the template from your workflows repo being changed at all recently, so super confused what is causing this failure.

I would have another look tomorrow because I don't have my laptop with me ATM (which I usually use to code), so everything feels very awkward :)

Another thread I haven't got to look at, not sure if it's related at all: Github Actions reusable workflow doesn't recognize secrets #69082

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it's related at all

don't think so, the test-scripts job doesn't have a reusable workflow

Copy link
Collaborator Author

@DanielYang59 DanielYang59 Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's related to secrets access from forked repos:

With the exception of GITHUB_TOKEN, secrets are not passed to the runner when a workflow is triggered from a forked repository.

As you might have noticed both this PR and #210 were created from our forks.

However above documentation doesn't provide any workaround for this :(

Perhaps skip that particular script (structure_viz.py) when running from fork?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds like it could be the culprit. probably the thing to do is to rather than fetch those structures on the fly, save them as JSON to tests/files/structs and load them from there in this script (while leaving the code that generated those structures files in place but behind if no os.path.isfile(...) as documentation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You always have helpful suggestions :) How is 95614f2 looking? I don't really like to manually download and save files or such, it's always better to automate this

@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 29, 2024

Notice another issue, both our test suite and pre-commit seems to target main branch in this PR's for some reason.

At commit 1f11e0b, main branch has NOT been merged (test_structure_viz.py has a total of 224 lines) but errors are reported on recently included change from main branch that hasn't yet been merged.


Update: Looks like something wrong with the "checkout" action perhaps actions/checkout#1359 leads to the following:

HEAD is now at 70d2c39 Merge 1f11e0b into ceb7fb6

i.e. checkout action would merge ref branch on its own.

@DanielYang59 DanielYang59 changed the title Clean up var names in unit tests Clean up var names in unit tests, avoid MP API access in GitHub workflow Sep 29, 2024
Copy link
Owner

@janosh janosh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot @DanielYang59! much clearer now, which vars are module-scoped 👍

examples/make_assets/structure_viz.py Outdated Show resolved Hide resolved
tests/files/structures/mp-12712.json Outdated Show resolved Hide resolved
@DanielYang59 DanielYang59 requested a review from janosh September 29, 2024 10:46
examples/make_assets/structure_viz.py Outdated Show resolved Hide resolved
examples/make_assets/structure_viz.py Outdated Show resolved Hide resolved
Copy link
Owner

@janosh janosh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for uncovering actions/checkout#1359. didn't know about this subtlety. will try to remember if it comes up again

@janosh janosh merged commit 2494ead into janosh:main Sep 29, 2024
22 checks passed
@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 29, 2024

No problem! Thanks for reviewing (always amazed by how much you know about all those APIs)

@DanielYang59 DanielYang59 deleted the interactive-unit-test branch September 29, 2024 10:57
@janosh
Copy link
Owner

janosh commented Sep 29, 2024

maybe time for a new release soon? what do you think? anything that should be done first?

@DanielYang59
Copy link
Collaborator Author

DanielYang59 commented Sep 29, 2024

Perhaps let me finish #200 perhaps in one or two days? It's largely done, I just want to finish the interactive tester and have a try on it?

But feel free to release a new one now and I'm happy to keep it slow and polish that more :)

@janosh
Copy link
Owner

janosh commented Sep 29, 2024

sure no hurry, we can definitely wait 2 days or more. looking forward to #200! 👍

@DanielYang59
Copy link
Collaborator Author

I really appreciate that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linting Linting & quality assurance testing Test all the things
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants