Collisions causing teleports

I'm following a pygame tutorial and am working on movements and collisions. The following code is used:

def move(self, speed):
        if self.direction.magnitude() != 0:
            self.direction = self.direction.normalize()

        self.hitbox.x += self.direction.x * speed
        self.collision('horizontal')
        self.hitbox.y += self.direction.y * speed
        self.collision('vertical')
        self.rect.center = self.hitbox.center


    def collision(self, direction):
        if direction == 'horizontal':
            for sprite in self.obstacle_sprites:
                if sprite.hitbox.colliderect(self.hitbox):
                    if self.direction.x > 0: # Moving right
                        self.hitbox.right = sprite.rect.left
                    if self.direction.x < 0: # Moving left
                        self.hitbox.left = sprite.rect.right

        if direction == 'vertical':
            for sprite in self.obstacle_sprites:
                if sprite.rect.colliderect(self.hitbox):
                    if self.direction.y > 0: # Moving down
                        self.hitbox.bottom = sprite.rect.top
                    if self.direction.y < 0: # Moving up
                        self.hitbox.top = sprite.rect.bottom

It seems pretty standard. Determine which side is making collisions by looking at the change in x or y, and move the player to the edge of the object. There are two issues with this.

The biggest problem is that when the player can occasionally teleport. If for example, the player is on the right side of an object and trying to move left and up (or left and down) at the same time, a collision can happen on that side, but the x check comes up false. Since the y value is definitely not zero, the player can teleport to the bottom of the obstacle when moving up and to the top of the obstacle when moving down, even though the collision is on the right or left side. It doesn't happen all of the time, but it's a problem fairly frequently.

The second problem is perhaps related to the first. The movement vector is normalized so that the player doesn't move faster than its typical speed when moving diagonally. However, due to the way the code is written, when you are attempting to move diagonally while colliding with an object such as a wall, the player moves at 0.707... speed in only one direction (instead of both as it would if there were no obstacles in the way).

How do I fix these issues? Obviously, the first problem is much more necessary to resolve than the slightly slower movement speed.

Edit: Success!!!

The player was essentially teleporting from the bottom corner to the top or from the top corner to the bottom. I realized that if I could prevent relocating the player's hitbox if the distance between the two sides was large, it should solve the problem.

if direction == 'vertical':
            for sprite in self.obstacle_sprites:
                if sprite.rect.colliderect(self.hitbox):
                    if self.direction.y > 0 and abs(self.hitbox.bottom - sprite.rect.top) < TILESIZE // 2: # Moving down
                        self.hitbox.bottom = sprite.rect.top
                    if self.direction.y < 0 and abs(self.hitbox.top - sprite.rect.bottom) < TILESIZE // 2: # Moving up
                        self.hitbox.top = sprite.rect.bottom

I measured the distance between the two sides, and the player only gets moved if the distance is essentially less than half the size of the obstacle.

I used abs() so that I didn't have to switch around the order of the objects and I used half the TILESIZE instead of a fixed number. On thinking about it, I probably could tie the distance to the speed instead since this check is being made every frame. Regardless, this stops the teleportation and fixes my problem! :)

I still don't know how to fix the second problem although that reduced speed can just be considered as a penalty for trying to move in a direction when there is an obstacle there. I'd still like to know how to fix it, however, if anyone has any ideas.

Edit again. The problem originally occurred because I was using sprite.rect instead of sprite.hitbox. The hitbox of the player was made slightly smaller than its rect, but the same also happened for the sprites. My solution works, but a much simpler and cleaner solution is changing every instance of sprite.rect with sprite.hitbox.