Tinche / aiofiles

File support for asyncio
Apache License 2.0
2.62k stars 151 forks source link

NamedTemporaryFile doesn't have ... a name? #152

Closed flowstef closed 1 year ago

flowstef commented 1 year ago

I rewrote some code using the synchronous tempfile.NamedTemporaryFile class from this

tmpfile = tempfile.NamedTemporaryFile()
logger.debug("Writing data to tmp file", file_name=tmpfile.name)

to this

tmpfile = aiofiles.tempfile.NamedTemporaryFile()
logger.debug("Writing data to tmp file", file_name=tmpfile.name)

I have to say I was surprised to see the following error:

AttributeError: 'AiofilesContextManager' object has no attribute 'name'

Am I missing something? Is there a way to access the name of the temporary file?

flowstef commented 1 year ago

The answer is yes, I was missing something. aiofiles.tempfile.NamedTemporaryFile() returns AiofilesContextManager, not tempfile.NamedTemporaryFile so it needs to be further unpacked before it can be used:

context_manager = aiofiles.tempfile.NamedTemporaryFile()
tmpfile = context_manager._obj

I hope this helps others who run into the same problem.

mateokurti commented 1 year ago

A little bit late to the party, but I'm running through the same problem. However, the context_manager._obj in my case is None. :/