combine L3961 and this to give:

```
if isinstance(f, Expr) and f.is_polynomial():
```