Skip to content

Commit a0a2c05

Browse files
Balandatfacebook-github-bot
authored andcommitted
Apply PEP 604 union type syntax codemod (#2561)
Summary: This codemods all `Optional[X]` type definitions to use the PEP 604 syntax `X | None` instead. I also ran ufmt to fix the imports. Why? I came across https://github.com/asottile/pyupgrade randomly and figured I'd give it a shot... This is mostly to test that everything runs smoothly. This may be kind of a pain to rebase on so not sure we should merge this in in one big chunk... Pull Request resolved: #2561 Reviewed By: esantorella Differential Revision: D63733395 Pulled By: Balandat fbshipit-source-id: 58e700487cb63223a4bc54a692567f0debd8f8a7
1 parent 68faeff commit a0a2c05

File tree

167 files changed

+1581
-1669
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+1581
-1669
lines changed

botorch/acquisition/acquisition.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import warnings
1212
from abc import ABC, abstractmethod
13-
from typing import Optional
1413

1514
import torch
1615
from botorch.exceptions import BotorchWarning
@@ -41,7 +40,7 @@ def __init__(self, model: Model) -> None:
4140
super().__init__()
4241
self.model: Model = model
4342

44-
def set_X_pending(self, X_pending: Optional[Tensor] = None) -> None:
43+
def set_X_pending(self, X_pending: Tensor | None = None) -> None:
4544
r"""Informs the acquisition function about pending design points.
4645
4746
Args:
@@ -115,7 +114,7 @@ class MCSamplerMixin(ABC):
115114

116115
_default_sample_shape = torch.Size([512])
117116

118-
def __init__(self, sampler: Optional[MCSampler] = None) -> None:
117+
def __init__(self, sampler: MCSampler | None = None) -> None:
119118
r"""Register the sampler on the acquisition function.
120119
121120
Args:

botorch/acquisition/active_learning.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
from __future__ import annotations
2525

26-
from typing import Optional
27-
2826
import torch
2927
from botorch import settings
3028
from botorch.acquisition.acquisition import AcquisitionFunction
@@ -53,9 +51,9 @@ def __init__(
5351
self,
5452
model: Model,
5553
mc_points: Tensor,
56-
sampler: Optional[MCSampler] = None,
57-
posterior_transform: Optional[PosteriorTransform] = None,
58-
X_pending: Optional[Tensor] = None,
54+
sampler: MCSampler | None = None,
55+
posterior_transform: PosteriorTransform | None = None,
56+
X_pending: Tensor | None = None,
5957
) -> None:
6058
r"""q-Integrated Negative Posterior Variance.
6159
@@ -140,7 +138,7 @@ def __init__(
140138
self,
141139
model: Model,
142140
objective: MCAcquisitionObjective,
143-
sampler: Optional[MCSampler] = None,
141+
sampler: MCSampler | None = None,
144142
) -> None:
145143
r"""Pairwise Monte Carlo Posterior Variance
146144

botorch/acquisition/analytic.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from abc import ABC
1717
from contextlib import nullcontext
1818
from copy import deepcopy
19-
from typing import Optional, Union
2019

2120
import torch
2221
from botorch.acquisition.acquisition import AcquisitionFunction
@@ -52,7 +51,7 @@ class AnalyticAcquisitionFunction(AcquisitionFunction, ABC):
5251
def __init__(
5352
self,
5453
model: Model,
55-
posterior_transform: Optional[PosteriorTransform] = None,
54+
posterior_transform: PosteriorTransform | None = None,
5655
) -> None:
5756
r"""Base constructor for analytic acquisition functions.
5857
@@ -76,14 +75,14 @@ def __init__(
7675
)
7776
self.posterior_transform = posterior_transform
7877

79-
def set_X_pending(self, X_pending: Optional[Tensor] = None) -> None:
78+
def set_X_pending(self, X_pending: Tensor | None = None) -> None:
8079
raise UnsupportedError(
8180
"Analytic acquisition functions do not account for X_pending yet."
8281
)
8382

8483
def _mean_and_sigma(
8584
self, X: Tensor, compute_sigma: bool = True, min_var: float = 1e-12
86-
) -> tuple[Tensor, Optional[Tensor]]:
85+
) -> tuple[Tensor, Tensor | None]:
8786
"""Computes the first and second moments of the model posterior.
8887
8988
Args:
@@ -135,8 +134,8 @@ class LogProbabilityOfImprovement(AnalyticAcquisitionFunction):
135134
def __init__(
136135
self,
137136
model: Model,
138-
best_f: Union[float, Tensor],
139-
posterior_transform: Optional[PosteriorTransform] = None,
137+
best_f: float | Tensor,
138+
posterior_transform: PosteriorTransform | None = None,
140139
maximize: bool = True,
141140
):
142141
r"""Single-outcome Probability of Improvement.
@@ -189,8 +188,8 @@ class ProbabilityOfImprovement(AnalyticAcquisitionFunction):
189188
def __init__(
190189
self,
191190
model: Model,
192-
best_f: Union[float, Tensor],
193-
posterior_transform: Optional[PosteriorTransform] = None,
191+
best_f: float | Tensor,
192+
posterior_transform: PosteriorTransform | None = None,
194193
maximize: bool = True,
195194
):
196195
r"""Single-outcome Probability of Improvement.
@@ -237,8 +236,8 @@ class qAnalyticProbabilityOfImprovement(AnalyticAcquisitionFunction):
237236
def __init__(
238237
self,
239238
model: Model,
240-
best_f: Union[float, Tensor],
241-
posterior_transform: Optional[PosteriorTransform] = None,
239+
best_f: float | Tensor,
240+
posterior_transform: PosteriorTransform | None = None,
242241
maximize: bool = True,
243242
) -> None:
244243
"""qPI using an analytic approximation.
@@ -314,8 +313,8 @@ class ExpectedImprovement(AnalyticAcquisitionFunction):
314313
def __init__(
315314
self,
316315
model: Model,
317-
best_f: Union[float, Tensor],
318-
posterior_transform: Optional[PosteriorTransform] = None,
316+
best_f: float | Tensor,
317+
posterior_transform: PosteriorTransform | None = None,
319318
maximize: bool = True,
320319
):
321320
r"""Single-outcome Expected Improvement (analytic).
@@ -378,8 +377,8 @@ class LogExpectedImprovement(AnalyticAcquisitionFunction):
378377
def __init__(
379378
self,
380379
model: Model,
381-
best_f: Union[float, Tensor],
382-
posterior_transform: Optional[PosteriorTransform] = None,
380+
best_f: float | Tensor,
381+
posterior_transform: PosteriorTransform | None = None,
383382
maximize: bool = True,
384383
):
385384
r"""Logarithm of single-outcome Expected Improvement (analytic).
@@ -447,9 +446,9 @@ class LogConstrainedExpectedImprovement(AnalyticAcquisitionFunction):
447446
def __init__(
448447
self,
449448
model: Model,
450-
best_f: Union[float, Tensor],
449+
best_f: float | Tensor,
451450
objective_index: int,
452-
constraints: dict[int, tuple[Optional[float], Optional[float]]],
451+
constraints: dict[int, tuple[float | None, float | None]],
453452
maximize: bool = True,
454453
) -> None:
455454
r"""Analytic Log Constrained Expected Improvement.
@@ -525,9 +524,9 @@ class ConstrainedExpectedImprovement(AnalyticAcquisitionFunction):
525524
def __init__(
526525
self,
527526
model: Model,
528-
best_f: Union[float, Tensor],
527+
best_f: float | Tensor,
529528
objective_index: int,
530-
constraints: dict[int, tuple[Optional[float], Optional[float]]],
529+
constraints: dict[int, tuple[float | None, float | None]],
531530
maximize: bool = True,
532531
) -> None:
533532
r"""Analytic Constrained Expected Improvement.
@@ -606,7 +605,7 @@ def __init__(
606605
X_observed: Tensor,
607606
num_fantasies: int = 20,
608607
maximize: bool = True,
609-
posterior_transform: Optional[PosteriorTransform] = None,
608+
posterior_transform: PosteriorTransform | None = None,
610609
) -> None:
611610
r"""Single-outcome Noisy Log Expected Improvement (via fantasies).
612611
@@ -762,8 +761,8 @@ class UpperConfidenceBound(AnalyticAcquisitionFunction):
762761
def __init__(
763762
self,
764763
model: Model,
765-
beta: Union[float, Tensor],
766-
posterior_transform: Optional[PosteriorTransform] = None,
764+
beta: float | Tensor,
765+
posterior_transform: PosteriorTransform | None = None,
767766
maximize: bool = True,
768767
) -> None:
769768
r"""Single-outcome Upper Confidence Bound.
@@ -812,7 +811,7 @@ class PosteriorMean(AnalyticAcquisitionFunction):
812811
def __init__(
813812
self,
814813
model: Model,
815-
posterior_transform: Optional[PosteriorTransform] = None,
814+
posterior_transform: PosteriorTransform | None = None,
816815
maximize: bool = True,
817816
) -> None:
818817
r"""Single-outcome Posterior Mean.
@@ -857,7 +856,7 @@ def __init__(
857856
self,
858857
model: Model,
859858
weights: Tensor,
860-
posterior_transform: Optional[PosteriorTransform] = None,
859+
posterior_transform: PosteriorTransform | None = None,
861860
) -> None:
862861
r"""Scalarized Posterior Mean.
863862
@@ -919,7 +918,7 @@ class PosteriorStandardDeviation(AnalyticAcquisitionFunction):
919918
def __init__(
920919
self,
921920
model: Model,
922-
posterior_transform: Optional[PosteriorTransform] = None,
921+
posterior_transform: PosteriorTransform | None = None,
923922
maximize: bool = True,
924923
) -> None:
925924
r"""Single-outcome Posterior Mean.
@@ -1135,8 +1134,8 @@ def _get_noiseless_fantasy_model(
11351134

11361135

11371136
def _preprocess_constraint_bounds(
1138-
acqf: Union[LogConstrainedExpectedImprovement, ConstrainedExpectedImprovement],
1139-
constraints: dict[int, tuple[Optional[float], Optional[float]]],
1137+
acqf: LogConstrainedExpectedImprovement | ConstrainedExpectedImprovement,
1138+
constraints: dict[int, tuple[float | None, float | None]],
11401139
) -> None:
11411140
r"""Set up constraint bounds.
11421141
@@ -1180,7 +1179,7 @@ def _preprocess_constraint_bounds(
11801179

11811180

11821181
def _compute_log_prob_feas(
1183-
acqf: Union[LogConstrainedExpectedImprovement, ConstrainedExpectedImprovement],
1182+
acqf: LogConstrainedExpectedImprovement | ConstrainedExpectedImprovement,
11841183
means: Tensor,
11851184
sigmas: Tensor,
11861185
) -> Tensor:

botorch/acquisition/bayesian_active_learning.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import warnings
2424

25-
from typing import Optional, Union
26-
2725
from botorch.acquisition.acquisition import AcquisitionFunction, MCSamplerMixin
2826
from botorch.acquisition.objective import PosteriorTransform
2927
from botorch.models import ModelListGP
@@ -79,10 +77,10 @@ class qBayesianActiveLearningByDisagreement(
7977
):
8078
def __init__(
8179
self,
82-
model: Union[ModelListGP, SaasFullyBayesianSingleTaskGP],
83-
sampler: Optional[MCSampler] = None,
84-
posterior_transform: Optional[PosteriorTransform] = None,
85-
X_pending: Optional[Tensor] = None,
80+
model: ModelListGP | SaasFullyBayesianSingleTaskGP,
81+
sampler: MCSampler | None = None,
82+
posterior_transform: PosteriorTransform | None = None,
83+
X_pending: Tensor | None = None,
8684
) -> None:
8785
"""
8886
Batch implementation [kirsch2019batchbald]_ of BALD [Houlsby2011bald]_,

botorch/acquisition/cached_cholesky.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from __future__ import annotations
1212

1313
import warnings
14-
from typing import Optional
1514

1615
import torch
1716
from botorch.acquisition.acquisition import MCSamplerMixin
@@ -72,7 +71,7 @@ def __init__(
7271
self,
7372
model: Model,
7473
cache_root: bool = False,
75-
sampler: Optional[MCSampler] = None,
74+
sampler: MCSampler | None = None,
7675
) -> None:
7776
r"""Set class attributes and perform compatibility checks.
7877

botorch/acquisition/cost_aware.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import warnings
1515
from abc import ABC, abstractmethod
16-
from typing import Callable, Optional, Union
16+
from collections.abc import Callable
1717

1818
import torch
1919
from botorch import settings
@@ -35,7 +35,7 @@ class CostAwareUtility(Module, ABC):
3535

3636
@abstractmethod
3737
def forward(
38-
self, X: Tensor, deltas: Tensor, sampler: Optional[MCSampler] = None
38+
self, X: Tensor, deltas: Tensor, sampler: MCSampler | None = None
3939
) -> Tensor:
4040
r"""Evaluate the cost-aware utility on the candidates and improvements.
4141
@@ -67,7 +67,7 @@ def __init__(self, cost: Callable[[Tensor, Tensor], Tensor]) -> None:
6767
self._cost_callable: Callable[[Tensor, Tensor], Tensor] = cost
6868

6969
def forward(
70-
self, X: Tensor, deltas: Tensor, sampler: Optional[MCSampler] = None
70+
self, X: Tensor, deltas: Tensor, sampler: MCSampler | None = None
7171
) -> Tensor:
7272
r"""Evaluate the cost function on the candidates and improvements.
7373
@@ -109,9 +109,9 @@ class InverseCostWeightedUtility(CostAwareUtility):
109109

110110
def __init__(
111111
self,
112-
cost_model: Union[DeterministicModel, GPyTorchModel],
112+
cost_model: DeterministicModel | GPyTorchModel,
113113
use_mean: bool = True,
114-
cost_objective: Optional[MCAcquisitionObjective] = None,
114+
cost_objective: MCAcquisitionObjective | None = None,
115115
min_cost: float = 1e-2,
116116
) -> None:
117117
r"""Cost-aware utility that weights increase in utility by inverse cost.
@@ -153,8 +153,8 @@ def forward(
153153
self,
154154
X: Tensor,
155155
deltas: Tensor,
156-
sampler: Optional[MCSampler] = None,
157-
X_evaluation_mask: Optional[Tensor] = None,
156+
sampler: MCSampler | None = None,
157+
X_evaluation_mask: Tensor | None = None,
158158
) -> Tensor:
159159
r"""Evaluate the cost function on the candidates and improvements. Note
160160
that negative values of `deltas` are instead scaled by the cost, and not

botorch/acquisition/decoupled.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import warnings
1212
from abc import ABC
13-
from typing import Optional
1413

1514
import torch
1615
from botorch.acquisition.acquisition import AcquisitionFunction
@@ -52,7 +51,7 @@ class DecoupledAcquisitionFunction(AcquisitionFunction, ABC):
5251
"""
5352

5453
def __init__(
55-
self, model: ModelList, X_evaluation_mask: Optional[Tensor] = None, **kwargs
54+
self, model: ModelList, X_evaluation_mask: Tensor | None = None, **kwargs
5655
) -> None:
5756
r"""Initialize.
5857
@@ -71,12 +70,12 @@ def __init__(
7170
self.X_pending = None
7271

7372
@property
74-
def X_evaluation_mask(self) -> Optional[Tensor]:
73+
def X_evaluation_mask(self) -> Tensor | None:
7574
r"""Get the evaluation indices for the new candidate."""
7675
return self._X_evaluation_mask
7776

7877
@X_evaluation_mask.setter
79-
def X_evaluation_mask(self, X_evaluation_mask: Optional[Tensor] = None) -> None:
78+
def X_evaluation_mask(self, X_evaluation_mask: Tensor | None = None) -> None:
8079
r"""Set the evaluation indices for the new candidate."""
8180
if X_evaluation_mask is not None:
8281
# TODO: Add batch support
@@ -92,8 +91,8 @@ def X_evaluation_mask(self, X_evaluation_mask: Optional[Tensor] = None) -> None:
9291

9392
def set_X_pending(
9493
self,
95-
X_pending: Optional[Tensor] = None,
96-
X_pending_evaluation_mask: Optional[Tensor] = None,
94+
X_pending: Tensor | None = None,
95+
X_pending_evaluation_mask: Tensor | None = None,
9796
) -> None:
9897
r"""Informs the AF about pending design points for different outcomes.
9998
@@ -135,7 +134,7 @@ def set_X_pending(
135134
self.X_pending = X_pending
136135
self.X_pending_evaluation_mask = X_pending_evaluation_mask
137136

138-
def construct_evaluation_mask(self, X: Tensor) -> Optional[Tensor]:
137+
def construct_evaluation_mask(self, X: Tensor) -> Tensor | None:
139138
r"""Construct the boolean evaluation mask for X and X_pending
140139
141140
Args:

0 commit comments

Comments
 (0)