Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Daniel J. Beutel
    @danieljanes
    Hi everyone, we've come across a mypy error that shouldn't be an error AFAICT. Shouldn't a function returning a Dict[str, str] be compatible with an argument that expects a function returning a Dict[str, Union[str, int]? Here's the full code example:
    from typing import Callable, Dict, Union
    
    class Foo:
        def __init__(self, bar_fn: Callable[[int], Dict[str, Union[str, int]]]):
            self.bar_fn = bar_fn
    
        def get_dict(self) -> Dict[str, Union[str, int]]:
            return self.bar_fn(1)
    
    
    def fn_1(x: int) -> Dict[str, Union[str, int]]:
        return {"key1": "value1", "key2": 2}
    
    
    Foo(bar_fn=fn_1)  # This works
    
    
    def fn_2(x: int) -> Dict[str, str]:
        return {"key": "value"}
    
    
    Foo(bar_fn=fn_2)  # This raises the following mypy error:
    # [filename].py:23: error: Argument "bar_fn" to "Foo" has incompatible type "Callable[[int], Dict[str, str]]"; expected "Callable[[int], Dict[str, Union[str, int]]]"
    4 replies
    Kyle Altendorf
    @altendky
    i'm looking at trying to catch cases where trio has a parameter defaulted to None and trio-stubs doesn't have that parameter hinted as Optional. https://github.com/python-trio/trio/blob/d203b30f807e43dc9b101de2e52355ebd10b757a/trio/_core/_run.py#L1942 -- https://github.com/python-trio/trio-typing/blob/f32f17b0f242daf2d42407f383ca581d64b6c299/trio-stubs/lowlevel.pyi#L97 i guessed that maybe stubtest would catch this, but it doesn't seem to. is there another approach i should use to find similar cases? or perhaps there is a reason this "can't" be done?
    Kyle Altendorf
    @altendky
    @ignormies:matrix.org that's the idea, yeah, but this is checking separate stubs, not a program using the trio library. in fact, having that and running mypy against qtrio which uses trio is exactly what turned this up. i haven't figured out how to get that to be enforced against the combination of package and stubs.
    ignormies
    @ignormies:matrix.org
    [m]

    trio has a parameter defaulted to None and trio-stubs doesn't have that parameter hinted as Optional

    In this context, would the problem be in trio, or in trio-stubs?

    Kyle Altendorf
    @altendky
    @ignormies:matrix.org trio-stubs. it ought to have clock: typing.Optional[trio.abc.Clock] = ...,
    but just reporting the discrepancy would leave me a chance to pick which to change. so for the goal of catching this scenario, it doesn't matter which is wrong, i think.
    mahmoudajawad
    @mahmoudajawad:matrix.org
    [m]
    How can I trace a possible bug where class attribute with name defaults would lose its typing when used in inherited class? This only applies to an attribute with the name defaults.
    RK
    @sonthonaxrk

    Hey there, how can I dissable the mypyc compilation when using the mypy command line?

    I'm trying to do some debugging of the source code, and I don't need it compiled. I just want to step through the code.

    4 replies
    Never mind, figured it out
    Bryan Forbes
    @bryanforbes
    @hauntsaninja I noticed that X as X is only re-exported for stubs… what happens in regular modules?
    It’s not really clear in the mypy release note blog post
    Kyle Altendorf
    @altendky
    ignormies
    @ignormies:matrix.org
    [m]
    So is it just not possible?
    Kyle Altendorf
    @altendky
    @hauntsaninja should stubtest check the package and the stubs against each other when it comes to implicit optional hints? context is 9 messages up, but trio has a parameter =None and the stubs have : trio.abc.Clock = .... seems like it would be good to have some tool or another able to find this discrepancy.
    RK
    @sonthonaxrk
    Is anyone here familiar with writting plugins for mypy?
    I'm trying to dynamically add a method to ca ClassDef in a plugin hook.
    What I can't work out is: how to construct new Function definitions.
    Because those function definitions need types in their args.
    But I don't know how to get arbitrary types
    RK
    @sonthonaxrk
    I know this is a really dumb question, but how can I construct mypy.nodes.Var objects?
    And mypy.nodes.TypeInfo
    This message was deleted
    RK
    @sonthonaxrk
    Sorry for thinking outloud
    RK
    @sonthonaxrk
    I think lookup_fully_qualified is what I need.
    It'd be nice if there was autodocumentation for the plugin api
    6 replies
    Henry Schreiner
    @henryiii
    :heart: MyPy 0.800 :heart:
    RK
    @sonthonaxrk
    @freundTech thanks for your help. I've actually managed to do something pretty cool with it.
    Basically, I was getting really annoyed with how a lot of python web developers just do ad hoc interfaces between services. And I wanted to see if I could create a metaprogrammed OpenAPI client; but one where I could also use MyPy to verify you are using the API correctly.
    Shantanu
    @hauntsaninja
    @bryanforbes agreed that the blog post is a little confusingly worded. PEP 484 was amended to say that only import X as X counts as an explicit re-export, not import X as Y. What happens in regular .py files depends on whether or not you have --implicit-reexport or not
    Bryan Forbes
    @bryanforbes
    Thanks! If the import X as X is included in __all__ in a regular .py file, will it be exported?
    Shantanu
    @hauntsaninja
    yeah, symbols in __all__ should be exported
    @altendky stubtest does check that default values are a subtype of the arg's type hint
    maybe the issue is that your stub is decorated?
    Bryan Forbes
    @bryanforbes
    @hauntsaninja thanks!
    Kyle Altendorf
    @altendky
    @hauntsaninja the stub uses ... for the default. it seems like the package default isn't checked against the stub hint. or at least isn't enforcing no implicit optional
    Shantanu
    @hauntsaninja
    @altendky can you remove the decorator and try?
    Kyle Altendorf
    @altendky
    @hauntsaninja i tried removing it and it didn't seem to make a difference. it sounds like you expect it ought to be caught so i'll make up a minimal example to try to confirm expectations and outcome. thanks.
    Kyle Altendorf
    @altendky
    @hauntsaninja https://gist.github.com/altendky/3327ab219a04e27f628b92aa6c480d8a here's the minimal example. i can certainly file a ticket if you think it is appropriate.
    also, this isn't a pressing issue. at this point i'm starting to just merge the stubs into the package so this is relevant primarily in the context of just improving (i think? :]) stubtest.
    Shantanu
    @hauntsaninja
    @altendky if i change your repro to create a pyi file instead of a py file in package-stubs, i get:
    error: package.f is inconsistent, runtime argument "p" has a default value of type None, which is incompatible with stub argument type builtins.int
    Stub: at line 1
    def (p: builtins.int =)
    Runtime: at line 1 in file /Users/shantanu/tmp/lol/package/__init__.py
    def (p=None)
    altendky @altendky sighs
    Kyle Altendorf
    @altendky
    bad repro, sorry.
    i'll explore the difference between that and the actual situation. thanks.
    Shantanu
    @hauntsaninja
    thanks for making a repro though! :-) let me know if there's something i could add to stubtest to make debugging easier
    DW
    @DW07045298_twitter
    Hi, can anyone help me understand what is wrong with my annotations on my code https://bpa.st/MGSOM . mypy thinks there is an error on L8 but I dont understand what is wrong. It looks like mypy correctly restricts to one type of a Union when it enters an if block but it throws that knowledge away when it enters a locally defined function (compare L5 & L7). Is this a mypy bug?
    DW
    @DW07045298_twitter
    from typing import Union
    
    def foo(X: Union[int, str]) -> bool:
        if isinstance(X, str):
            reveal_type(X)  # str
            def g(y: int) -> bool:
                reveal_type(X)  # Union[int, str]
                return len(X) > y  # <<<<< error: Argument 1 to "len" has incompatible type "Union[int, str]"; expected "Sized"
    
            return g(10)
    
        reveal_type(X)  # int
        return bool(X)
    Jahnavi-Jonnalagadda
    @Jahnavi-Jonnalagadda
    Hi There! How can we use an if statement in a for loop statement?
    Anyone please
    Shantanu
    @hauntsaninja