Skip to content

PYI055 suggestion causes TypeError at runtime #6455

@adamtheturtle

Description

@adamtheturtle

ruff 0.0.283 (error is new in this version)
Python 3.11.4

I get an error from ruff which proposes a solution, and applying that proposal causes an error.

I have the following file:

# example.py
#
# pip install requests_mock httpretty

import httpretty
import requests_mock

item: type[requests_mock.Mocker] | type[httpretty] = requests_mock.Mocker
  • python example.py has no output
  • ruff --select=PYI --isolated example.py shows:
example.py:42:7: PYI055 Multiple `type` members in a union. Combine them into one, e.g., `type[requests_mock.Mocker | httpretty]`.

The ruff documentation at https://beta.ruff.rs/docs/rules/unnecessary-type-union/ suggests that this change is purely cosmetic:

The type built-in function accepts unions, and it is clearer to explicitly specify them as a single type.

However, if I make the change suggested, and then run python example.py, I get a TypeError:

Traceback (most recent call last):
  File "/Users/adam/Desktop/mypy-ruff-example/example.py", line 38, in <module>
    item: type[requests_mock.Mocker | httpretty] = requests_mock.Mocker
               ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
TypeError: unsupported operand type(s) for |: 'type' and 'module'

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions