botorch icon indicating copy to clipboard operation
botorch copied to clipboard

[Feature Request] Enable type checkers

Open AdrianSosic opened this issue 1 year ago • 6 comments

🚀 Feature Request

BoTorch is pretty much completely typed but currently not usable with type checkers when installed as package into other environments, because there is no py.typed file. This currently requires silencing warnings raised by tools like mypy and thus effectively prevents people from using the available type information when integrating BoTorch into projects. How about activating this feature?

AdrianSosic avatar Oct 11 '24 13:10 AdrianSosic

We've been contemplating using pyre to do type checking and we'd really like to do that, unfortunately in the past this has caused a lot of headaches due to pyre not working well with pytorch, which resulted in many (thousands!) of spurious errors that we had to manually silence. I haven't looked into this more recently, maybe the situation is better now - if it is or there is an efficient way to deal with this that doesn't require splattering ignores all across the codebase we'd be happy to use type checking in development and the CI.

I guess that is technically a separate question from adding a py.typed file, but given that there currently would likely be a good number of type errors there is a question of the value of doing this.

cc @esantorella, @saitcakmak who probably have additional thoughts on this.

Balandat avatar Oct 11 '24 14:10 Balandat

Hi @Balandat, thanks for the quick reply. I know the hassle with pytorch (in fact, we've also globally silenced interaction warnings with it) but I think we need to distinguish between two things here:

  • One is the type checks within botorch, where the problematic interaction with torch occurs as you describe it
  • The other is the use of the existing type information (i.e. purely on a botorch level) by the users of your package

So how about a more pragmatic approach? Instead of fixing pytorch issues, you could simply ignore them entirely (or in whatever reasonable way) but still make your own type hints readable by external tools. Or am I misunderstanding the problem?

AdrianSosic avatar Oct 11 '24 15:10 AdrianSosic

So how about a more pragmatic approach? Instead of fixing pytorch issues, you could simply ignore them entirely (or in whatever reasonable way) but still make your own type hints readable by external tools. Or am I misunderstanding the problem?

No I don't think you're misunderstanding the problem. I guess since we haven't enforced type checking due to the pytorch challenges I know that botorch itself has some typing issues and isn't internally consistent. So I would hesitate to make those erroneous type hints readable by other tools before fixing it. So probably the thing we should do is to (i) silence interaction warnings with pytorch in botorch, (ii) fix any issues with botorch type hints, and (iii) make the type hints readable. This will require a bit of work but it should be worth it.

QQ: how exactly are you silencing the interaction warnings with pytorch in your setup?

Balandat avatar Oct 11 '24 16:10 Balandat

I see, thanks for clarification 👍🏼

And sorry for confusion: I just noticed that we only ignored gpytorch imports in our mypy.ini. It seems for pytorch we don't get any warnings by default (actually not sure whether that's because everything is OK or because it's not checked in the first place 🤔 need to investigate 😄 )

AdrianSosic avatar Oct 14 '24 07:10 AdrianSosic

@AdrianSosic as of #2982 we do have a py.typed file - we enabled type checking in botorch itself but will look into how painful this becomes :)

Balandat avatar Sep 17 '25 13:09 Balandat

Hi @Balandat. Thanks for the ping. I see that it's now available via the new release since our pipelines start to complain 😄 I'll have a look at it now and will get back to you in case I find some unresolved problems 👍🏼

AdrianSosic avatar Oct 24 '25 10:10 AdrianSosic