Bound TypeVar on generic methods inside generic class









up vote
1
down vote

favorite












For some reason, this code lints up as a problem:



from typing import *
T = TypeVar("T", bound="Foo")
S = TypeVar("S")

class Foo(Generic[S]):
@classmethod
def func(cls: Type[T]) -> T:
return cls()


Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.



Is this a bug? What am I doing wrong?



I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.










share|improve this question

























    up vote
    1
    down vote

    favorite












    For some reason, this code lints up as a problem:



    from typing import *
    T = TypeVar("T", bound="Foo")
    S = TypeVar("S")

    class Foo(Generic[S]):
    @classmethod
    def func(cls: Type[T]) -> T:
    return cls()


    Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.



    Is this a bug? What am I doing wrong?



    I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      For some reason, this code lints up as a problem:



      from typing import *
      T = TypeVar("T", bound="Foo")
      S = TypeVar("S")

      class Foo(Generic[S]):
      @classmethod
      def func(cls: Type[T]) -> T:
      return cls()


      Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.



      Is this a bug? What am I doing wrong?



      I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.










      share|improve this question













      For some reason, this code lints up as a problem:



      from typing import *
      T = TypeVar("T", bound="Foo")
      S = TypeVar("S")

      class Foo(Generic[S]):
      @classmethod
      def func(cls: Type[T]) -> T:
      return cls()


      Mypy linter sends me to the def func line, saying Unsupported type Type["T"]. This does not happen if Foo is not defined as a generic class.



      Is this a bug? What am I doing wrong?



      I'm using S for different methods, and I wish to use T and Type[T] later on inside subclasses of Foo.







      python-3.x generics typing type-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 26 at 12:01









      Bharel

      6,19411638




      6,19411638






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.






          share|improve this answer




















          • Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
            – Bharel
            Jun 29 at 13:26










          • @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
            – Michael0x2a
            Jun 30 at 1:14










          • In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
            – Michael0x2a
            Jun 30 at 1:16










          • More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
            – Michael0x2a
            Jun 30 at 1:18










          • Thank you very much for the extended explanation! You're awesome!
            – Bharel
            Jun 30 at 8:43











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51042452%2fbound-typevar-on-generic-methods-inside-generic-class%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.






          share|improve this answer




















          • Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
            – Bharel
            Jun 29 at 13:26










          • @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
            – Michael0x2a
            Jun 30 at 1:14










          • In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
            – Michael0x2a
            Jun 30 at 1:16










          • More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
            – Michael0x2a
            Jun 30 at 1:18










          • Thank you very much for the extended explanation! You're awesome!
            – Bharel
            Jun 30 at 8:43















          up vote
          2
          down vote



          accepted










          I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.






          share|improve this answer




















          • Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
            – Bharel
            Jun 29 at 13:26










          • @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
            – Michael0x2a
            Jun 30 at 1:14










          • In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
            – Michael0x2a
            Jun 30 at 1:16










          • More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
            – Michael0x2a
            Jun 30 at 1:18










          • Thank you very much for the extended explanation! You're awesome!
            – Bharel
            Jun 30 at 8:43













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.






          share|improve this answer












          I believe this is a bug in mypy. Unfortunately, the best workaround for now is to just add a # type: ignore annotation to that line, perhaps along with a link to the relevant issue. Later, you can check if that warning has been fixed by running mypy with the --warn-unused-ignores flag.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 28 at 13:12









          Michael0x2a

          21.8k1672125




          21.8k1672125











          • Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
            – Bharel
            Jun 29 at 13:26










          • @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
            – Michael0x2a
            Jun 30 at 1:14










          • In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
            – Michael0x2a
            Jun 30 at 1:16










          • More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
            – Michael0x2a
            Jun 30 at 1:18










          • Thank you very much for the extended explanation! You're awesome!
            – Bharel
            Jun 30 at 8:43

















          • Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
            – Bharel
            Jun 29 at 13:26










          • @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
            – Michael0x2a
            Jun 30 at 1:14










          • In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
            – Michael0x2a
            Jun 30 at 1:16










          • More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
            – Michael0x2a
            Jun 30 at 1:18










          • Thank you very much for the extended explanation! You're awesome!
            – Bharel
            Jun 30 at 8:43
















          Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
          – Bharel
          Jun 29 at 13:26




          Apart from remembering all of mypy's issues, how do you find if things like this are a bug or not? Searching github isn't always easy for these issues.
          – Bharel
          Jun 29 at 13:26












          @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
          – Michael0x2a
          Jun 30 at 1:14




          @Bharel -- In general, mypy's type system is designed to be relatively straightforward. Of course, there are a few gotchas here and there, but if something feels like a bug, it usually is. (The second most common scenario is that you're looking for a feature that hasn't been implemented yet.)
          – Michael0x2a
          Jun 30 at 1:14












          In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
          – Michael0x2a
          Jun 30 at 1:16




          In this case, I read your snippet and agreed the error made no sense. I then found the relevant issue by searching "typevar classmethod" -- that narrowed down the number of issues to about. The reason I tacked on the keyword "classmethod" was because I happened to know that mypy currently struggles a little when working with decorators. You used a classmethod in your snippet, so I suspected that was likely partially the culprit. (If I were less lazy, I would have also tried repro-ing your issue after removing the @classmethod to confirm my hunch.)
          – Michael0x2a
          Jun 30 at 1:16












          More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
          – Michael0x2a
          Jun 30 at 1:18




          More broadly, you can try just asking on the gitter or filing an issue -- that's what I do when I'm not sure if something's a bug/if I can't find an open issue. Of course, sometimes I'm wrong and the code is fine/I missed the issue, but no biggie, mistakes happen. The mypy core devs encourage people to ask questions using the issue tracker anyways.
          – Michael0x2a
          Jun 30 at 1:18












          Thank you very much for the extended explanation! You're awesome!
          – Bharel
          Jun 30 at 8:43





          Thank you very much for the extended explanation! You're awesome!
          – Bharel
          Jun 30 at 8:43


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51042452%2fbound-typevar-on-generic-methods-inside-generic-class%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20