{"record":{"id":"0739b551daf163d4","repo":"apache/beam","slug":"compression-type-must-be-compressiontype-object-but-was-s-0739b5","errorCode":null,"errorMessage":"compression_type must be CompressionType object but was %s","messagePattern":"compression_type must be CompressionType object but was (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/filesystem.py","lineNumber":153,"sourceCode":"\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\n\n    if self.readable():\n      self._read_size = read_size","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/filesystem.py#L135-L171","documentation":"CompressedFile only accepts compression types from the CompressionTypes enum that denote an actual codec (GZIP, BZIP2, etc.). Passing a non-member object (e.g. a string 'gzip') fails CompressionTypes.is_valid_compression_type and raises TypeError.","triggerScenarios":"filesystem.CompressedFile(fh, compression_type='gzip') or any value not in CompressionTypes (other than AUTO/UNCOMPRESSED, which fail with a different error).","commonSituations":"Reading the compression type from config/CLI as a raw string and passing it unconverted; confusing this API with gzip.open which takes strings.","solutions":["Pass a CompressionTypes enum member, e.g. CompressionTypes.GZIP or CompressionTypes.BZIP2.","Convert string input via getattr(CompressionTypes, value.upper()) or a mapping dict.","Use filesystems.FileSystems.open() which handles compression types automatically instead of constructing CompressedFile manually."],"exampleFix":"// before\nCompressedFile(fh, compression_type='gzip')\n# after\nfrom apache_beam.io.filesystem import CompressionTypes\nCompressedFile(fh, compression_type=CompressionTypes.GZIP)","handlingStrategy":"validation","validationCode":"if not CompressionTypes.is_valid_compression_type(ctype):\n    raise TypeError(f'unsupported compression_type: {ctype!r}')","typeGuard":"def is_compression_type(v) -> bool:\n    return CompressionTypes.is_valid_compression_type(v)","tryCatchPattern":"try:\n    reader = CompressedFile(fh, compression_type=ctype)\nexcept TypeError:\n    ctype = getattr(CompressionTypes, str(ctype).upper(), CompressionTypes.GZIP)\n    reader = CompressedFile(fh, compression_type=ctype)","preventionTips":["Always use CompressionTypes enum members, never raw strings.","Centralize string-to-enum conversion in one helper."],"tags":["python","apache-beam","io","type-mismatch"],"backgroundTag":"type-mismatch","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"}