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

Support openpyxl 3.x #779

Merged
merged 1 commit into from
Jan 7, 2025
Merged

Support openpyxl 3.x #779

merged 1 commit into from
Jan 7, 2025

Conversation

reinhardt
Copy link
Contributor

No description provided.

@reinhardt reinhardt requested a review from ale-rt January 7, 2025 09:41
Copy link
Member

@ale-rt ale-rt left a comment

Choose a reason for hiding this comment

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

Is this to support Python 3.11?
In this case please also add the proper GHA tests

@@ -252,4 +249,9 @@ def __call__(self):
"Content-Type",
"application/vnd.openxmlformats-" "officedocument.spreadsheetml.sheet",
)
return save_virtual_workbook(book)

with NamedTemporaryFile(delete=False) as tmp:
Copy link
Member

Choose a reason for hiding this comment

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

It seems strange to me that we have delete False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's modelled after this example: https://docs.python.org/3/library/tempfile.html#examples

Note that on python 3.13 the parameter is delete_on_close which is a more accurate name.

Copy link
Member

Choose a reason for hiding this comment

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

Fine for the example, but who is taking care of cleaning the tmp file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The context manager is doing that. The parameter only prevents deletion when close() is called. When the context is exited the file is deleted anyway.

The documentation is a bit ambiguous, but that's how I understand it.

If delete is true (the default), the file is deleted as soon as it is closed.

https://docs.python.org/3.11/library/tempfile.html#tempfile.NamedTemporaryFile

In 3.13 there is an improvement:

If delete is true and delete_on_close is false, the file is deleted on context manager exit only

https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meh... I tested it - the file is not cleaned up by the context manager. I'll change the code.

@@ -252,4 +249,9 @@ def __call__(self):
"Content-Type",
"application/vnd.openxmlformats-" "officedocument.spreadsheetml.sheet",
)
return save_virtual_workbook(book)

with NamedTemporaryFile(delete=False) as tmp:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
with NamedTemporaryFile(delete=False) as tmp:
with NamedTemporaryFile() as tmp:

Comment on lines 255 to 257
tmp.close()
with open(tmp.name, mode="rb") as f:
return f.read()
Copy link
Member

@ale-rt ale-rt Jan 7, 2025

Choose a reason for hiding this comment

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

Suggested change
tmp.close()
with open(tmp.name, mode="rb") as f:
return f.read()
tmp.seek(0)
return f.read()

I am not really sure if the tmp.seek(0) is needed, but closing and reopening the file feels quite wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It feels right to me, because we're first writing and then reading. That's different modes.

@reinhardt reinhardt merged commit c67d01c into main Jan 7, 2025
2 checks passed
@reinhardt reinhardt deleted the openpyxl3 branch January 7, 2025 12:44
@ale-rt
Copy link
Member

ale-rt commented Jan 7, 2025

@reinhardt why did you merge this, it was not approved and I asked for changes...

@@ -218,11 +219,7 @@ def create_workbook(self):
value = module.title
if value is not None:
cell = sheet.cell(row=row, column=column)
if key == "number":
# force sting
cell.set_explicit_value(value)
Copy link
Member

Choose a reason for hiding this comment

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

I think this was needed.

Copy link
Member

Choose a reason for hiding this comment

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

See 3765183

@reinhardt
Copy link
Contributor Author

I did implement all your requested changes.

@ale-rt
Copy link
Member

ale-rt commented Jan 7, 2025

I did implement all your requested changes.

Note true:

Please complete the work and next time wait for an approval

@reinhardt
Copy link
Contributor Author

GHA tests for python 3.11 are here: #780

#779 (comment)

This was added after the PR was merged. That said, I can look into it again, but I doubt that it's needed. The method set_explicit_value doesn't even exist in newer openpyxl versions. It used to allow a data_type parameter, but we're not even passing that.

See https://foss.heptapod.net/openpyxl/openpyxl/-/blob/1a12044865c82190bdfb39b1c02b7411b3f52d00/openpyxl/cell/cell.py#L254

@ale-rt
Copy link
Member

ale-rt commented Jan 8, 2025

GHA tests for python 3.11 are here: #780

They are failing.

#779 (comment)

This was added after the PR was merged. That said, I can look into it again, but I doubt that it's needed. The method set_explicit_value doesn't even exist in newer openpyxl versions. It used to allow a data_type parameter, but we're not even passing that.

That means we use the default.

See https://foss.heptapod.net/openpyxl/openpyxl/-/blob/1a12044865c82190bdfb39b1c02b7411b3f52d00/openpyxl/cell/cell.py#L254

I already checked that yesterday.
What is your point?

@reinhardt
Copy link
Contributor Author

We don't need the case distiction.

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 this pull request may close these issues.

2 participants