In any human interaction, it’s natural to tell the other person what you think they want to hear. We don’t like to risk offending by offering unwelcome news or opinion. (That’s why, for example, a lot of internal polling is so useless.)

Even the most powerful people are not immune from this tendency. Hence Barack Obama yesterday in Senegal, telling his hosts – not falsely, but perhaps gilding the lily a bit – that they were a beacon for African democracy: “There are free and fair elections, repeated transfers of power – peacefully – a vibrant civil society, a strong press, and dozens of political parties.”

He went on to praise the recent African renaissance, saying that Africa and the US “have shared values, such as democracy, freedom, human rights, good governance” – although he acknowledged that more needs to be done on all of these fronts.

But what was noteworthy was the president’s willingness to tell the Senegalese government something it didn’t want to hear: that gay people should be included within the compass of human rights. Asked about Wednesday’s decision in the US Supreme Court that overturned the Defence of Marriage Act, Obama said this:

The issue of gays and lesbians, and how they’re treated, has come up and has been controversial in many parts of Africa.  So I want the African people just to hear what I believe, and that is that every country, every group of people, every religion have different customs, different traditions.  And when it comes to people’s personal views and their religious faith, et cetera, I think we have to respect the diversity of views that are there.

But when it comes to how the state treats people, how the law treats people, I believe that … regardless of race, regardless of religion, regardless of gender, regardless of sexual orientation, when it comes to how the law treats you, how the state treats you – the benefits, the rights and the responsibilities under the law – people should be treated equally.  And that’s a principle that I think applies universally.

This is a message that badly needs to be heard in Africa. Although Senegalese president Sall was perhaps not entirely truthful when he said that “homosexuals are not being prosecuted, persecuted,” Senegal is definitely not one of the worst offenders in this regard. Across Africa and the Middle East, gays and lesbians still risk ostracism, imprisonment and even death for exercising basic human rights.

In the past, however, the United States has failed to offer any comfort to the oppressed on issues like this. The idea that sexual freedom is a fundamental right is not just controversial in benighted Africa or among Muslim fundamentalists; it is also deeply contested in the west, and especially in the US.

So under the Bush administration, while foreign countries were periodically lectured about the need for democracy and human rights (sometimes sincerely, sometimes not), it was always clear that those things did not extend to gay rights, gender equality and reproductive freedom. Instead, the US conspired at the United Nations with the Vatican and fundamentalist regimes to actively frustrate those goals.

The cognitive dissonance involved, for example, in telling Middle Eastern countries that they should emancipate their women, but not to the extent of letting them choose whether or not to have children, cannot have helped the American crusade for democracy in the region. (Although to be fair, it had plenty of other problems as well.) And in Africa, some of the worst anti-gay measures have enjoyed the covert support of hard-line Republicans.

Obama’s forthrightness in Senegal is therefore indicative of a major step forward. It’s by no means all it could be; puritanism is still a powerful force in America, and just last week the Supreme Court had to pull the administration into line over its policy of trying to stop NGOs supporting legalisation of prostitution.

More has to be done before the US is seen as a consistent friend of liberty and before those who are persecuted for their gender or their sexuality, at home or abroad, can feel confident of its support. But this week the Supreme Court and the president have both helped to move things in the right direction.