{"record":{"id":"fefd62073dc90476","repo":"apache/beam","slug":"file-object-must-not-be-none","errorCode":null,"errorMessage":"File object must not be None","messagePattern":"File object must not be None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/filesystem.py","lineNumber":150,"sourceCode":"        return compression_type\n    return cls.UNCOMPRESSED\n\n\nclass CompressedFile(object):\n  \"\"\"File wrapper for easier handling of compressed files.\"\"\"\n  # XXX: This class is not thread safe in the read path.\n\n  # The bit mask to use for the wbits parameters of the zlib compressor and\n  # decompressor objects.\n  _gzip_mask = zlib.MAX_WBITS | 16  # Mask when using GZIP headers.\n\n  def __init__(\n      self,\n      fileobj: BinaryIO,\n      compression_type=CompressionTypes.GZIP,\n      read_size=DEFAULT_READ_BUFFER_SIZE):\n    if not fileobj:\n      raise ValueError('File object must not be None')\n\n    if not CompressionTypes.is_valid_compression_type(compression_type):\n      raise TypeError(\n          'compression_type must be CompressionType object but '\n          'was %s' % type(compression_type))\n    if compression_type in (CompressionTypes.AUTO,\n                            CompressionTypes.UNCOMPRESSED):\n      raise ValueError(\n          'Cannot create object with unspecified or no compression')\n\n    self._file = fileobj\n    self._compression_type = compression_type\n\n    if self._file.tell() != 0:\n      raise ValueError(\n          'File object must be at position 0 but was %d' % self._file.tell())\n    self._uncompressed_position = 0\n    self._uncompressed_size: Optional[int] = None","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/filesystem.py#L132-L168","documentation":"CompressedFile (the base for compressed file readers/writers in apache_beam.io.filesystem) requires a real binary file object to wrap. Its __init__ rejects falsy file objects (None, or objects whose __bool__ is False) because there is nothing to compress or decompress against.","triggerScenarios":"Calling filesystem.CompressedFile(None) or passing the result of an open()/gcsio call that returned None (e.g. a failed open stubbed to return None).","commonSituations":"A helper function returns None instead of a file object on failure and the result is passed straight to CompressedFile; mocks in tests returning None.","solutions":["Pass a valid binary-mode file object (e.g. open(path, 'rb') or filesystem.open(...)).","Check why the producer returned None — fix the failed open or error handling upstream.","Assert the file object is not None before constructing CompressedFile."],"exampleFix":"# before\nfh = maybe_open(path)\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)\n# after\nfh = maybe_open(path)\nassert fh is not None, f'failed to open {path}'\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)","handlingStrategy":"validation","validationCode":"if fh is None:\n    raise ValueError(f'failed to open file for {path}')\nreader = CompressedFile(fh, compression_type=CompressionTypes.GZIP)","typeGuard":"def is_fileobj(obj) -> bool:\n    return obj is not None and hasattr(obj, 'read') and hasattr(obj, 'tell')","tryCatchPattern":"try:\n    reader = CompressedFile(fh, compression_type=ctype)\nexcept ValueError as e:\n    if 'File object must not be None' in str(e):\n        fh = open(path, 'rb')\n        reader = CompressedFile(fh, compression_type=ctype)\n    else:\n        raise","preventionTips":["Never let helper functions return None on open failure — raise instead.","Assert file objects are non-None at API boundaries."],"tags":["python","apache-beam","io","null"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}