An attendee holds a handmade “Lambslide” sign during an election night rally for Conor Lamb, Democratic candidate for the House, in Canonsburg, Pa., on Tuesday. (Andrew Harrer/Bloomberg News)

Democrat Conor Lamb’s slim lead in a Pennsylvania House district that President Trump carried by 20 percentage points in 2016 holds some important lessons for both parties as they look toward November.

As a national proposition, this election year is shaping up as a referendum on an unpopular president and the party in power, which guarantees that Republicans will find themselves in many tough races.

The special election to fill an open congressional seat in southwestern Pennsylvania, where absentee ballots that will determine the outcome were still being counted on Wednesday morning, should not have been one of them. What made the difference was the uneven match of the two men running.

Lamb, a charismatic 33-year-old former Marine officer and prosecutor, was fresh and appealing. He stressed his connection with the conservative district, rarely talking about Trump and keeping his distance from the leftward-moving national Democratic Party. His opening campaign ad featured him firing an AR-15 semiautomatic rifle.

In other parts of the country, Lamb might not have survived a Democratic primary. For this district, however, he was nearly perfectly situated to capitalize on the enthusiasm of the Democratic resistance without alienating swing voters who had gone for Trump. Still, a reasonably talented Republican most likely would have beaten him easily for a seat that the GOP has held since 2002.

Instead, the Republicans picked state Rep. Rick Saccone, a politician of such finesse that on the eve of the election, he accused “the other side” of hating their president, their country and their God. Yes, God.

Saccone made few public appearances. His record in the legislature was solidly anti-labor, in a district where nearly 90,000 people live in union households. Pennsylvania AFL-CIO President Richard W. Bloomingdale told me unions would not have jumped into the race with the intensity they did had the Republicans picked just about any other candidate.

“The Saccone campaign was a joke. It was worse than a joke. It was an embarrassment,” said Corry Bliss, who heads the Congressional Leadership Fund, a super PAC aligned with House Speaker Paul D. Ryan (R-Wis.). “We are not going to win with joke campaigns and awful candidates.” And this, mind you, is the assessment of someone whose organization poured more than $3.5 million into the race on Saccone’s behalf.

Trump was not much help, either, though he came to the district twice and dispatched family members and aides there, too. The rally he held at the Pittsburgh airport Saturday night — where the president preened for more than an hour before inviting Saccone up to share the stage with him — may have done more to energize Democrats than bring out supporters on behalf of a hapless Republican congressional candidate who boasted of having been “Trump before Trump was Trump.” That is something other Republicans no doubt will remember when the White House calls with an offer to bring the road show to their districts.

Lamb’s strong performance, meanwhile, is mixed news for House Democratic leader Nancy Pelosi (D-Calif.). Republicans tried to make the race all about her, a strategy they used to devastating effect in a special congressional election last year in Georgia and one that they plan to employ against other Democrats straight through November.

That it did not work is perhaps a sign that Pelosi will not be as toxic as Republicans hope. But Lamb also put the question away by declaring that, if he were elected, he would not vote for her as leader. You are likely to hear the same from other Democratic candidates in close races this year, which would be an ominous sign for the prospects of Pelosi hanging on to the top job, whether or not her party regains the House.

This was, of course, just one election in a unique part of the country — not necessarily indicative of the forces that will be at play all across the map in November. And this particular district will cease to exist, thanks to a court order that the state’s legislative lines be redrawn for more partisan balance.

But with primary season getting underway, there is at least one important reminder that Pennsylvania’s 18th District offered the rest of the country: It all starts with picking the right person to run.