Skip to content

Feature request: option that completely disallows typing.cast #17077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wyattscarpenter opened this issue Mar 30, 2024 · 11 comments
Open

Feature request: option that completely disallows typing.cast #17077

wyattscarpenter opened this issue Mar 30, 2024 · 11 comments

Comments

@wyattscarpenter
Copy link
Contributor

Feature

An option that disallows use of typing.cast, eg --disallow-casts

Pitch

cast() gets around static type safety without ensuring dynamic type correctness. Sometimes you have to use it—you know how it is—but it's not ideal. (For instance, mypy documentation seems to imply the main use of cast is when the typechecker or type annotations aren't quite good enough.) That means, in my opinion, cast is a great candidate to have a option disallowing its use, to ensure code quality in codebases where it is not needed.

I also think this option should be enabled in strict mode, but I suspect other people might disagree.

This proposal is distinct from the --warn-redundant-casts option that already exists, but is somewhat similar.

@AlexWaygood
Copy link
Member

See also #5756

@andre-qumulo
Copy link

@AlexWaygood issue 5756 hasn't had any movement in 3 years, a check like this would allow us to be more mindful of when cast is used. With multiple contributors to a project it is an additional burden on reviewers to check their cast use, a linter that fails by default would help ease that burden.

@erictraut
Copy link

@andre-qumulo, @wyattscarpenter, a feature like this would require standardization and an update to the Python typing spec. As such, the mypy issue tracker probably isn't the best place to be discussing the issue.

If you'd like to propose a new mechanism in the type system, here is the process to get such a change ratified. You are welcome to start a discussion thread in the Python typing forum.

@wyattscarpenter
Copy link
Contributor Author

@erictraut, by "a feature like this" are you referring to the #5756 safe cast feature (makes sense to me) or the #17077 mypy-disallow-cast-flag feature (harder for me to see why)?

@erictraut
Copy link

Yeah, I should have been clearer there. Apologies for any confusion. I was referring to the "safe cast" feature that @andre-qumulo refers to above. The "mypy-disallow-cast-flag" makes sense as a typechecker-specific option.

@wyattscarpenter
Copy link
Contributor Author

Thanks, good to know.

@andre-qumulo
Copy link

@erictraut I was referring to a "mypy-disallow-cast-flag". Can we have that until the "safe cast" feature is discussed/prioritized/completed? I'd like to prevent away team devs from accidentally adding cast to my team's codebase.

@wyattscarpenter
Copy link
Contributor Author

@andre-qumulo, I think @erictraut understood your original message as voicing support for the disallow-cast flag, but also noticed your interest in the safe-cast feature and wanted to provide you with direction in case you wanted to make safe-casts a reality. (It sounds like no one's working on it right now and so you might have to spearhead it yourself, using the process he described.)

@andre-qumulo
Copy link

Ah, my misunderstanding. My original comment was that referring this request to the safe-cast feature is a great way of ignoring this one, but the safe-cast hasn't been touched in 3 years. Right now a disallow-cast flag would be really helpful.

@wyattscarpenter
Copy link
Contributor Author

Hopefully both suggestions get implemented some day :)

Myself, for the moment, my "80/20" solution is to grep for "\bcast(". Not perfect by any means, but sometimes it works.

@andre-qumulo
Copy link

Ah, good point, I might just implement that myself in our code base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants