python union类型_python – “预期类型’Union [str,bytearray]’得到’int’而不是写入方法中的警告...

我的脚本使用预先生成的数据模式按块写入文件块:

# Data pattern generator

def get_random_chunk_pattern():

return ''.join(random.choice(ascii_uppercase + digits + ascii_lowercase) for _ in range(8))

….

# DedupChunk class CTOR:

class DedupChunk:

def __init__(self, chunk_size, chunk_pattern, chunk_position=0, state=DedupChunkStates.PENDING):

self._chunk_size = chunk_size # chunk size in bytes

self._chunk_pattern = chunk_pattern

self._chunk_position = chunk_position

self._state = state

self.mapping = None

@property

def size(self):

return self._chunk_size

@property

def pattern(self):

return self._chunk_pattern

@property

def position(self):

return self._chunk_position

@property

def state(self):

return self._state

….

# Here Chunk object is being initialized (inside other class's CTOR):

chunk_size = random.randint(64, 192) * 1024 # in bytes

while (position + chunk_size) < self.file_size: # generating random chunks number

self.chunks.append(DedupChunk(chunk_size, DedupChunkPattern.get_random_chunk_pattern(), position))

….

# Actual writing

with open(self.path, 'rb+') as f:

for chunk in self.chunks:

f.write(chunk.pattern * (chunk.size // 8))

PyCharm显示“预期类型’Union [str,bytearray]’得到’int’而不是”写入方法中的警告

但在删除分裂时

f.write(chunk.pattern * chunk.size),或者在外面进行划分:

chunk.size //= 8

f.write(chunk.pattern * chunk.size)

警告消失了

这里到底发生了什么?

谢谢

你可能感兴趣的:(python,union类型)