-
Notifications
You must be signed in to change notification settings - Fork 249
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
IDEA: inheriting from 'Any' should create a new 'Any' type #567
Comments
In our internal test code we are often using "fakes" (a bit like mocks, only with canned values and behaviour and no dynamic behaviour) in place of real domain objects that are difficult to set up: # Implementation:
class Foo: # domain class with side effects
...
def do_interesting_stuff(foo: Foo): ...
# Test
class FakeFoo: # simple class
...
foo = FakeFoo()
do_interesting_stuff(cast(Foo, foo)) If I understand the proposal correctly, this would allow us to instead derive |
Yes, but by inheriting from Any with a type ignore comment will accomplish
the same thing.
…On Fri, Jun 22, 2018, 13:51 Sebastian Rittau ***@***.***> wrote:
In our internal test code we are often using "fakes" (a bit like mocks,
only with canned values and behaviour and no dynamic behaviour) in place of
real domain objects that are difficult to set up:
# Implementation:class Foo: # domain class with side effects
...
def do_interesting_stuff(foo: Foo): ...
# Testclass FakeFoo: # simple class
...
foo = FakeFoo()
do_interesting_stuff(cast(Foo, foo))
If I understand the proposal correctly, this would allow us to instead
derive FakeFoo from Any and do away with the cast? This would be helpful.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#567 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMkaN5wq5Pi6hCGrPYXJIIq45Mce9ks5t_VjPgaJpZM4U0Y8o>
.
|
I like this idea. We can preserve the current behaviour for unimported bases etc., while have the both-ways- @JukkaL what do you think? |
I'm not sure if this is important enough for the extra complexity to work on at least in the near term. This would add a new kind of type, and from experience we know that these sorts of changes tend to result in all kinds of unanticipated corner cases that may take a lot of work to get right, even for supposedly simple features. They also complicate the internals of all type checking tools, all/most of which are still lagging behind PEP 484 in supported features (including mypy). For non-stub code, the fact that the |
On one hand this may be just |
What is a particular use case when this is useful? |
Is this still relevant? In typeshed we derive |
As nice as this would be, I fear it may break too much code. I think this is definitely something that should be experimented on in a type checker first. |
Currently, in mypy,
class M(Any):
is an error, becauseAny
is not an actual class. (Note that this is different fromclass M(A):
whereA
has typeAny
. I'm talking about explicitly inheriting fromAny
only here.)In mypy, if we ignore the error, we obtain a class that is a subclass of all other classes. (At runtime it still fails, but in a stub file this will work.) Example:
However,
M
does not have the other important property ofAny
, that it is a superclass of all other classes. If we add the following to the above example, we get an error:As a feature, it might be nice to allow explicitly subclassing
Any
and make the resulting class behave more likeAny
, so that thebar(A())
call above actually works.Why would this be useful? Why not just make
M
an object with typeAny
? Because we can still endowM
with specific methods, which will be type-checked as usual:If
M
were just an object with typeAny
, we wouldn't get that error.Real-world use case: Mock; see the thread started at python/typeshed#2173 (comment).
The text was updated successfully, but these errors were encountered: