Skip to content

WPS432 should ignore literal types #3397

@phagenlocher

Description

@phagenlocher

Rule request

(I am not sure whether this is a bug or a rule request, sorry if I picked the wrong one.)

Thesis

WPS432 currently is applied to magic numbers within literal types.

foobar: Literal[9001] = 9001

In this example, 9001 inside of Literal is being reported. This behavior should be changed such that this type annotations are ignored.

Reasoning

With how the WPS rules currently work, you are allowed to create a Literal with a str constant, but not with an int or float.

foobar: Literal[9001] = 9001 # not allowed
barfoo: Literal["something"] = "something" # allowed

The only way to have my typed constant is to either use noqa or use int and float as the type, which don't give me the same static guarantees than Literal.

More generally, a magic number in the domain of types is different from a magic number in the domain of values. In the case of Literal the semantic meaning of the number is given by the way it is used to build the whole type. I think this opens up the question how literals should be treated within type annotations.

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