Skip to content

Commit 686e536

Browse files
authored
Replace %s with f-strings (#4671)
Replace %s with f-strings Closes #4632
1 parent c62100c commit 686e536

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

pymc3/model.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def get_context(cls, error_if_none=True) -> Optional[T]:
184184
# Calling code expects to get a TypeError if the entity
185185
# is unfound, and there's too much to fix.
186186
if error_if_none:
187-
raise TypeError("No %s on context stack" % str(cls))
187+
raise TypeError(f"No {cls} on context stack")
188188
return None
189189
return candidate
190190

@@ -200,9 +200,9 @@ def get_contexts(cls) -> List[T]:
200200
# no race-condition here, contexts is a thread-local object
201201
# be sure not to override contexts in a subclass however!
202202
context_class = cls.context_class
203-
assert isinstance(context_class, type), (
204-
"Name of context class, %s was not resolvable to a class" % context_class
205-
)
203+
assert isinstance(
204+
context_class, type
205+
), f"Name of context class, {context_class} was not resolvable to a class"
206206
if not hasattr(context_class, "contexts"):
207207
context_class.contexts = threading.local()
208208

@@ -223,15 +223,14 @@ def resolve_type(c: Union[Type, str]) -> Type:
223223
c = getattr(modules[cls.__module__], c)
224224
if isinstance(c, type):
225225
return c
226-
raise ValueError("Cannot resolve context class %s" % c)
226+
raise ValueError(f"Cannot resolve context class {c}")
227227

228228
assert cls is not None
229229
if isinstance(cls._context_class, str):
230230
cls._context_class = resolve_type(cls._context_class)
231231
if not isinstance(cls._context_class, (str, type)):
232232
raise ValueError(
233-
"Context class for %s, %s, is not of the right type"
234-
% (cls.__name__, cls._context_class)
233+
f"Context class for {cls.__name__}, {cls._context_class}, is not of the right type"
235234
)
236235
return cls._context_class
237236

@@ -334,7 +333,7 @@ def logpt(self):
334333
else:
335334
logp = self.logp_sum_unscaledt
336335
if self.name is not None:
337-
logp.name = "__logp_%s" % self.name
336+
logp.name = f"__logp_{self.name}"
338337
return logp
339338

340339
@property
@@ -345,7 +344,7 @@ def logp_nojact(self):
345344
else:
346345
logp = at.sum(self.logp_nojac_unscaledt)
347346
if self.name is not None:
348-
logp.name = "__logp_%s" % self.name
347+
logp.name = f"__logp_{self.name}"
349348
return logp
350349

351350

@@ -709,7 +708,7 @@ def logp_dlogp_function(self, grad_vars=None, tempered=False, **kwargs):
709708
else:
710709
for i, var in enumerate(grad_vars):
711710
if var.dtype not in continuous_types:
712-
raise ValueError("Can only compute the gradient of continuous types: %s" % var)
711+
raise ValueError(f"Can only compute the gradient of continuous types: {var}")
713712
# We allow one to pass the random variable terms as arguments
714713
if hasattr(var.tag, "value_var"):
715714
grad_vars[i] = var.tag.value_var
@@ -756,7 +755,7 @@ def logpt(self):
756755

757756
logp_var = at.sum([at.sum(factor) for factor in factors])
758757
if self.name:
759-
logp_var.name = "__logp_%s" % self.name
758+
logp_var.name = f"__logp_{self.name}"
760759
else:
761760
logp_var.name = "__logp"
762761
return logp_var
@@ -786,7 +785,7 @@ def logp_nojact(self):
786785
logp_var = at.sum([at.sum(factor) for factor in factors])
787786

788787
if self.name:
789-
logp_var.name = "__logp_nojac_%s" % self.name
788+
logp_var.name = f"__logp_nojac_{self.name}"
790789
else:
791790
logp_var.name = "__logp_nojac"
792791
return logp_var
@@ -990,10 +989,10 @@ def shape_from_dims(self, dims):
990989
for dim in dims:
991990
if dim not in self.coords:
992991
raise ValueError(
993-
"Unknown dimension name '%s'. All dimension "
992+
f"Unknown dimension name '{dim}'. All dimension "
994993
"names must be specified in the `coords` "
995994
"argument of the model or through a pm.Data "
996-
"variable." % dim
995+
"variable."
997996
)
998997
shape.extend(np.shape(self.coords[dim]))
999998
return tuple(shape)
@@ -1361,7 +1360,7 @@ def add_random_variable(self, var, dims: Optional[Tuple[Union[str, None], ...]]
13611360

13621361
@property
13631362
def prefix(self):
1364-
return "%s_" % self.name if self.name else ""
1363+
return f"{self.name}_" if self.name else ""
13651364

13661365
def name_for(self, name):
13671366
"""Checks if name has prefix and adds if needed"""
@@ -1581,16 +1580,16 @@ def check_start_vals(self, start):
15811580
valid_keys = ", ".join(self.named_vars.keys())
15821581
raise KeyError(
15831582
"Some start parameters do not appear in the model!\n"
1584-
"Valid keys are: {}, but {} was supplied".format(valid_keys, extra_keys)
1583+
f"Valid keys are: {valid_keys}, but {extra_keys} was supplied"
15851584
)
15861585

15871586
initial_eval = self.point_logps(point=elem)
15881587

15891588
if not np.all(np.isfinite(initial_eval)):
15901589
raise SamplingError(
15911590
"Initial evaluation of model at starting point failed!\n"
1592-
"Starting values:\n{}\n\n"
1593-
"Initial evaluation results:\n{}".format(elem, str(initial_eval))
1591+
f"Starting values:\n{elem}\n\n"
1592+
f"Initial evaluation results:\n{initial_eval}"
15941593
)
15951594

15961595
def check_test_point(self, *args, **kwargs):

0 commit comments

Comments
 (0)