Skip to content

Commit

Permalink
typing: Use Self from typing_extensions (#2494)
Browse files Browse the repository at this point in the history
Summary:
<!--
Thank you for sending the PR! We appreciate you spending the time to make BoTorch better.

Help us understand your motivation by explaining why you decided to make this change.

You can learn more about contributing to BoTorch here: https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md
-->

## Motivation
Why wait till 3.11 :) The dependancy of `typing_extensions` is already present in `torch` and is a core python development library with the same license as python itself.

### Have you read the [Contributing Guidelines on pull requests](https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md#pull-requests)?

Yes

Pull Request resolved: #2494

Test Plan:
I believe this only needs to pass the lint checks. However as it adds a new dependancy (which should be installed already with `torch`), there could be issues with action runners if installation of dependencies is done in a non-standard manner. [However it seems like it should be fine.](https://github.com/pytorch/botorch/blob/d52205a031ecd83b9ef73ba05ca990a284179edb/.github/workflows/test.yml#L45)

## Related PRs

* Closes #2487  (issue, not PR)

Reviewed By: saitcakmak

Differential Revision: D61976244

Pulled By: Balandat

fbshipit-source-id: be3dc67147a1bd210d336e56488f3c6c25b67939
  • Loading branch information
eddiebergman authored and facebook-github-bot committed Sep 16, 2024
1 parent 18eb95a commit ec2ad88
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
8 changes: 4 additions & 4 deletions botorch/models/approximate_gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import copy
import warnings

from typing import Optional, TypeVar, Union
from typing import Optional, Union

import torch
from botorch.exceptions.warnings import UserInputWarning
Expand Down Expand Up @@ -68,9 +68,9 @@
)
from torch import Tensor
from torch.nn import Module
from typing_extensions import Self


TApproxModel = TypeVar("TApproxModel", bound="ApproximateGPyTorchModel")
TRANSFORM_WARNING = (
"Using an {ttype} transform with `SingleTaskVariationalGP`. If this "
"model is trained in minibatches, a {ttype} transform with learnable "
Expand Down Expand Up @@ -132,11 +132,11 @@ def __init__(
def num_outputs(self):
return self._desired_num_outputs

def eval(self: TApproxModel) -> TApproxModel:
def eval(self) -> Self:
r"""Puts the model in `eval` mode."""
return Module.eval(self)

def train(self: TApproxModel, mode: bool = True) -> TApproxModel:
def train(self, mode: bool = True) -> Self:
r"""Put the model in `train` mode.
Args:
Expand Down
18 changes: 5 additions & 13 deletions botorch/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from abc import ABC, abstractmethod
from collections import defaultdict
from collections.abc import Mapping
from typing import Any, Callable, Optional, TYPE_CHECKING, TypeVar, Union
from typing import Any, Callable, Optional, TYPE_CHECKING, Union

import numpy as np
import torch
Expand All @@ -37,12 +37,11 @@
from gpytorch.likelihoods.gaussian_likelihood import FixedNoiseGaussianLikelihood
from torch import Tensor
from torch.nn import Module, ModuleDict, ModuleList
from typing_extensions import Self

if TYPE_CHECKING:
from botorch.acquisition.objective import PosteriorTransform # pragma: no cover

TFantasizeMixin = TypeVar("TFantasizeMixin", bound="FantasizeMixin")


class Model(Module, ABC):
r"""Abstract base class for BoTorch models.
Expand Down Expand Up @@ -289,11 +288,7 @@ def __init__(self, args):
"""

@abstractmethod
def condition_on_observations(
self: TFantasizeMixin,
X: Tensor,
Y: Tensor,
) -> TFantasizeMixin:
def condition_on_observations(self, X: Tensor, Y: Tensor) -> Self:
"""
Classes that inherit from `FantasizeMixin` must implement
a `condition_on_observations` method.
Expand Down Expand Up @@ -322,16 +317,13 @@ def transform_inputs(
a `transform_inputs` method.
"""

# When Python 3.11 arrives we can start annotating return types like
# this as
# 'Self', but at this point the verbose 'T...' syntax is needed.
def fantasize(
self: TFantasizeMixin,
self,
X: Tensor,
sampler: MCSampler,
observation_noise: Optional[Tensor] = None,
**kwargs: Any,
) -> TFantasizeMixin:
) -> Self:
r"""Construct a fantasy model.
Constructs a fantasy model in the following fashion:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ scipy
mpmath>=0.19,<=1.3
torch>=2.0.1
pyro-ppl>=1.8.4
typing_extensions
gpytorch==1.13
linear_operator==0.5.3

0 comments on commit ec2ad88

Please sign in to comment.