Skip to content

fix: initialise _file_base/_ext when file_name is None#308

Open
gaoflow wants to merge 1 commit into
Kludex:mainfrom
gaoflow:fix/file-none-attr-error-on-flush-to-disk
Open

fix: initialise _file_base/_ext when file_name is None#308
gaoflow wants to merge 1 commit into
Kludex:mainfrom
gaoflow:fix/file-none-attr-error-on-flush-to-disk

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 25, 2026

Copy link
Copy Markdown

Summary

File.__init__ only assigned _file_base and _ext inside the
if file_name is not None branch. When a File is created without a
filename (the documented case for application/octet-stream uploads,
where file_name defaults to None), those attributes are never set.
Any call to _get_disk_file() that reads them then raises an
AttributeError:

  • UPLOAD_KEEP_EXTENSIONS=Trueself._ext.decode(...)AttributeError: 'File' object has no attribute '_ext'
  • UPLOAD_KEEP_FILENAME=True with a dir → self._file_base + self._extAttributeError: 'File' object has no attribute '_file_base'

Both paths are reachable when the memory threshold is exceeded
(MAX_MEMORY_FILE_SIZE) during a nameless upload.

Fix

Move the assignments outside the conditional and supply empty-bytes
defaults for the None branch:

if file_name is not None:
    basename = os.path.basename(file_name)
    base, ext = os.path.splitext(basename)
else:
    base, ext = b"", b""
self._file_base = base
self._ext = ext

Two regression tests are added to tests/test_file.py.


This pull request was prepared with the assistance of AI, under my direction and review.

Review in cubic

File.__init__ only set _file_base and _ext inside the
`if file_name is not None` branch, leaving them undefined for
nameless uploads (e.g. application/octet-stream).  Any subsequent
call to _get_disk_file() with UPLOAD_KEEP_EXTENSIONS=True or
UPLOAD_KEEP_FILENAME=True triggered an AttributeError.

Initialise both attributes to empty bytes outside the conditional so
they are always present, and cover the regression with two tests.
@codspeed-hq

codspeed-hq Bot commented Jun 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 5 untouched benchmarks


Comparing gaoflow:fix/file-none-attr-error-on-flush-to-disk (b3055b7) with main (98080c5)

Open in CodSpeed

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

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.

1 participant