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

TypeError in _get_potcar_symbols() #2363

Closed
janosh opened this issue Jan 20, 2022 · 8 comments · Fixed by #2365
Closed

TypeError in _get_potcar_symbols() #2363

janosh opened this issue Jan 20, 2022 · 8 comments · Fixed by #2365

Comments

@janosh
Copy link
Member

janosh commented Jan 20, 2022

class Lobsterin()._get_potcar_symbols() causes CI errors in atomate

potcar = Potcar.from_file(POTCAR_input)
for pot in potcar:
if pot.potential_type != "PAW":
raise OSError("Lobster only works with PAW! Use different POTCARs")
# Warning about a bug in lobster-4.1.0
with zopen(POTCAR_input, "r") as f:
data = f.read()
if "SHA256" in data or "COPYR" in data:
warnings.warn(
"These POTCARs are not compatible with "
"Lobster up to version 4.1.0."
"\n The keywords SHA256 and COPYR "
"cannot be handled by Lobster"
" \n and will lead to wrong results."
)
if potcar.functional != "PBE":
raise OSError("We only have BASIS options for PBE so far")
Potcar_names = [name["symbol"] for name in potcar.spec]
return Potcar_names

        with zopen(POTCAR_input, "r") as f:
            data = f.read()
>       if "SHA256" in data or "COPYR" in data:
E       TypeError: a bytes-like object is required, not 'str'

Triggered by PR: hackingmaterials/atomate#738

@JaGeo
Copy link
Member

JaGeo commented Jan 20, 2022

Will have a look at it as soon as I am back from my vacation ... (On Monday)

It's just there to warn users if they use the POTCARs including these keywords. If you know how to fix it, please go ahead. Errors seem to be new, though...

And, thanks for putting all the work into this. 🙂

@janosh
Copy link
Member Author

janosh commented Jan 20, 2022

I guess one way to fix it would be

- if "SHA256" in data or "COPYR" in data: 
+ if b"SHA256" in data or b"COPYR" in data: 

but I was a bit confused by this error as I didn't expect the result f.read() to be a bytes object in the first place. Since data is prob supposed to be str, turning "SHA256" into bytes string wouldn't be the right fix here.

@JaGeo
Copy link
Member

JaGeo commented Jan 20, 2022

Yeah, same. I hope it is not again some test data problem in atomate. 🙈

@janosh
Copy link
Member Author

janosh commented Jan 20, 2022

Don't think so. Since zopen is coming from monty maybe @shyuep can comment how to handle this.

@JaGeo
Copy link
Member

JaGeo commented Jan 20, 2022

At least in pymatgen, the tests seem to be working:

def test_get_potcar_symbols(self):

@shyuep
Copy link
Member

shyuep commented Jan 20, 2022

zopen does not do anything to the encoding. It merely passes through to io.open or gzip or bzip2 based on the file extension. So this is not related to zopen. In general, python has become more strict on binary vs text types. So you might have to properly handle the encoding conversions.

@janosh
Copy link
Member Author

janosh commented Jan 20, 2022

@JaGeo Thanks, yes I saw the tests pass here. The difference is prob that in atomate the POTCAR is gzipped so the returned IO stream comes from gzip.open() instead of python's open().

@shyuep You mean check the type and handle the comparison accordingly?

if isinstance(data, bytes):
    if b"SHA256" in data or b"COPYR" in data:
        # ...
else:
    if "SHA256" in data or "COPYR" in data: 

@shyuep
Copy link
Member

shyuep commented Jan 20, 2022

No, you should handle the encoding of "data" first. If it is supposed to be a string, it needs to be converted to unicode first. The comparison should be based on unicode, not bytes. Then everything that follows using data should treat it as a proper unicode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants