The expected type Union [str, bytearray] 'received an' int 'instead of a warning in the write method

My script writes to a piece of the file with a piece, using pre-generated data templates:

#  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 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))

PyCharmdisplays a warning " Expected type 'Union[str, bytearray]' got 'int' instead" in the write method

But when deleting the division in f.write(chunk.pattern * chunk.size)or make the division outside:

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

warning disappeared

What really happened here?

thanks

+4
source share
1 answer

. IDE (- ) , , . , , -, int, int, , .

, IDE, chunk.pattern, ( ).

.

class DedupChunk:
    """
    :type _chunk_pattern: str
    ... other fields
    """
    ... # rest of class
+13

Source: https://habr.com/ru/post/1613392/


All Articles