/-- The affine cover of a scheme. -/
def affineCover (X : Scheme) : OpenCover X where
  J := X.carrier
  obj x := Spec.obj <| Opposite.op (X.local_affine x).choose_spec.choose
  map x :=
    ((X.local_affine x).choose_spec.choose_spec.some.inv ≫ X.toLocallyRingedSpace.ofRestrict _ : _)
  f x := x
  IsOpen x := by
    apply (config := { allowSynthFailures := true }) PresheafedSpace.IsOpenImmersion.comp
    apply PresheafedSpace.IsOpenImmersion.ofRestrict
  Covers := by
    intro x
    erw [coe_comp]
    

Proof state:
C : Type u
inst✝¹ : Category.{v, u} C
X✝ Y Z : Scheme
𝒰 : OpenCover X✝
f : X✝ ⟶ Z
g : Y ⟶ Z
inst✝ : ∀ (x : 𝒰.J), HasPullback (OpenCover.map 𝒰 x ≫ f) g
X : Scheme
x : ↑↑X.toPresheafedSpace
⊢ x ∈
    Set.range
      (⇑(LocallyRingedSpace.ofRestrict X.toLocallyRingedSpace
                (_ :
                  OpenEmbedding
                    ⇑(Opens.inclusion
                        (Exists.choose
                            (_ :
                              ∃ U R,
                                Nonempty
                                  (LocallyRingedSpace.restrict X.toLocallyRingedSpace
                                      (_ : OpenEmbedding ⇑(Opens.inclusion U.obj)) ≅
                                    Spec.toLocallyRingedSpace.obj (op R)))).obj))).val.base ∘
        ⇑(Nonempty.some
                  (_ :
                    Nonempty
                      (LocallyRingedSpace.restrict X.toLocallyRingedSpace
                          (_ :
                            OpenEmbedding
                              ⇑(Opens.inclusion
                                  (Exists.choose
                                      (_ :
                                        ∃ U R,
                                          Nonempty
                                            (LocallyRingedSpace.restrict X.toLocallyRingedSpace
                                                (_ : OpenEmbedding ⇑(Opens.inclusion U.obj)) ≅
                                              Spec.toLocallyRingedSpace.obj (op R)))).obj)) ≅
                        Spec.toLocallyRingedSpace.obj
                          (op
                            (Exists.choose
                              (_ :
                                ∃ R,
                                  Nonempty
                                    (LocallyRingedSpace.restrict X.toLocallyRingedSpace
                                        (_ :
                                          OpenEmbedding
                                            ⇑(Opens.inclusion
                                                (Exists.choose
                                                    (_ :
                                                      ∃ U R,
                                                        Nonempty
                                                          (LocallyRingedSpace.restrict X.toLocallyRingedSpace
                                                              (_ : OpenEmbedding ⇑(Opens.inclusion U.obj)) ≅
                                                            Spec.toLocallyRingedSpace.obj (op R)))).obj)) ≅
                                      Spec.toLocallyRingedSpace.obj (op R)))))))).inv.val.base)

Next tactic:
rw [Set.range_comp, Set.range_iff_surjective.mpr, Set.image_univ]