Open stereobutter opened 5 years ago
I am quite sure there was a very similar feature request but can't find it. I think a better idea is to add a plugin hook to selectively suppress some error codes (now that we have them) on a given line. Otherwise it sounds too niche.
Yes, I agree that this is a special case of "there is this error that I know I want to ignore". For this a plugin that gets called at the very end, when everything else is done, would be very useful. Is there already a way to filter out error messages/error codes in the plugin hooks that are already available?
Well, technically I can't say no, because I would be lying, but I really think you should not do this, better wait for the public API. The error manager is exposed though private type checker API ctx.api.msg.errors
but trying to do something there is super-fragile.
Even if this were not fragile (at the moment), I wonder to which symbol one would attach such a plugin to, isinstance
or Either
?
@SaschaSchlemmer get_function_hook()
for isinstance()
. But this is the last thing I say here :-)
@ilevkivskyi As discussed in the pull request about ignoring errors by regex, I would like to help you with improving the plugin system, so errors could be ignored from a plugin.
First I need to get more familiar with the plugin system of mypy. I will learn more about it in the next days. However my current idea would be to add a hook, which is called every time when an error should reported. This hook can than decide, if the error should be ignored or not.
For this the hook would need the info
object of the error, which holds many useful information to do this decision.
However, this would mean to introduce a new hook and to make the class of the info
object public for plugins. When I understand your discussion in this thread correctly, both are think you normally avoid, right? Do you have another approach in mind?
Thanks for your assistance here, André
Should this have the label topic-pep-604
? This seems like it might given other open issues with that label.
For developing an Either-like monad that types correctly and also behaves nicely at runtime I currently use the following pattern to independently declare the types for mypy and the runtime implementation
During static analysis it is important that
Left
andRight
get treated as aliases forEither
so that assignments get typechecked correctly (even though at runtime they are subclasses (and thus subtypes) ofEither
). Additionally I'd like to be able and useisinstance
checks at runtime to determine whether a result is an instance ofLeft
orRight
(or more general if something is an instance ofEither
). This mostly works (assignments typecheck and runtime behavior is as excepted), however mypy (rightfully) complains about theisinstance
checks thatIn cases like these it would be useful if one could tell mypy that a generic class can be used with instance checks. This would also be somewhat similar to how one can declare protocol classes to support runtime instance checks with the
@runtime_checkable
decorator.This would also allow user defined generics to behave similar to types from typing like
List
wherevs
I'd propose that
@runtime_checkable
marks anGeneric
accordingly so that in the example it would beAddendum
The issue is actually not with
Either
per se but rather with the type aliasesLeft
andRight
. For mypyactually is
I still think this would be a useful feature to be able and better support the dynamic nature of Python with the
if TYPE_CHECKING
pattern.